Join 3,497 readers in helping fund MetaFilter (Hide)


Strategy for removing hardcoded links from a Wordpress theme?
August 28, 2012 3:36 AM   Subscribe

Strategy for removing hardcoded links from a Wordpress theme?

I have a Wordpress site installed in a subfolder: test.com/dev which I will soon be moving to live.

The problem I am having is that in my theme folder, in some of my templates there are hardcoded URLs (e.g. in my header and some other template files), so when I move the site those links will be broken.

What's a good strategy for fixing this issue before I move the site from dev to live? Should I recursively search and replace the theme directory to remove hardcoded links when my site move is imminent?

Is there another strategy I should try?

Many thanks
posted by radiocontrolled to Computers & Internet (13 answers total) 1 user marked this as a favorite
 
I usually dump the database to an .sql file. Then I open the file in Textmate and do a Find and Replace. Then I import the new database in.

This post by Yoast links to a Search and Replace function that should help, too.
posted by backwards guitar at 3:52 AM on August 28, 2012


Check out the official docs on Referencing Files From a Template. Basically you use built-in PHP functions such as
get_bloginfo('template_url')
to get around these problems.
posted by Foci for Analysis at 3:55 AM on August 28, 2012


Sorry, I read the question wrong (although you may need to follow my advice too). foci has it. A lot of text editors will let you do a search and replace on a project or folder, so you can go through all the templates and replace the hard coded links with the proper function reference.
posted by backwards guitar at 4:00 AM on August 28, 2012


A follow-up, what build-in PHP function should be used for dynamically creating relative navigation links, so that links I create to pages, categories and posts are retained? These links would be coded in the header.php for my navigation.

Thanks so much for the help!
posted by radiocontrolled at 4:14 AM on August 28, 2012


A follow-up, what build-in PHP function should be used for dynamically creating relative navigation links

WordPress handles this natively with Custom Navigation Menus. Click the pages you want on your menu to add them, drag to re-order. All the links are relative, or will update when you change the installation's base URL/
posted by DarlingBri at 4:24 AM on August 28, 2012


DarlineBri, thanks. Unfortunately, we're not using custom navigation menus - while I think we will switch to them in the future, currently the menu is made from scratch.
posted by radiocontrolled at 4:31 AM on August 28, 2012


It will take six minutes to do it now.
posted by DarlingBri at 4:41 AM on August 28, 2012 [1 favorite]


A follow-up, what build-in PHP function should be used for dynamically creating relative navigation links, so that links I create to pages, categories and posts are retained?

See Good Navigation Links for examples.
posted by Foci for Analysis at 4:49 AM on August 28, 2012


Though I have to say that DarlingBri's approach is a better long term solution as it makes it really easy to modify your menu links.
posted by Foci for Analysis at 4:53 AM on August 28, 2012


So in WordPress template files: e.g. header.php index.php front-page.php single.php, page.php usually I do:
	<img src="<?php bloginfo('stylesheet_directory'); ?>/images/file.png" />
Or
	<style>
	.whatever { background-image: url(<?php bloginfo('stylesheet_directory'); ?>/images/file.png); }
	</style>
In style.css You only need to do:
	.whatever { background-image: url(images/file.png); }
Is that what you mean?
posted by artlung at 10:19 AM on August 28, 2012 [1 favorite]


Search And Replace
posted by spilon at 12:37 PM on August 28, 2012


Root Relative URLs plugin

Read the installation instructions
posted by Sharcho at 2:09 PM on August 29, 2012


spilon and Sharcho, both the plugins you suggest operate on user content, and not on theme files. They're potentially useful, but do not apply (to my reading of their documentation) to the question being asked, which was specifically about theme template files.
posted by artlung at 2:46 PM on August 29, 2012


« Older I'm the middle of some very st...   |  Super last minute plea for hel... Newer »
This thread is closed to new comments.