2

Please read this carefully as it is not what you might be expecting.

I'm building my site in a development sub domain. I don't want to have to go through the complications of running find and replace plugins after moving the site to the live area (as this can often go wrong on some serialized data).

I want to enter the urls into pages as they're going to be on the live site (they need to be absolute URLs for the RSS feeds. So I need a method with PHP or Javascript to find and replace http://mysite.com with http://dev.mysite.com on the output pages. This way I can still test and preview the site and when it comes to going live I just have to uncomment that code. I'm not worried about the URL in the settings as that is always easy to change.

I hope that makes sense.

Thanks.

2
  • That won't work, especially for scripts, this would mean that you mess up the overall intended load order. So technically it works but it's highly likely that if it works it will break things.
    – hakre
    Feb 9, 2011 at 17:21
  • Who keeps downvoting questions with no explanation as to how the questions could be improved? This is a perfectly valid question IMHO.
    – editor
    Feb 9, 2011 at 17:41

4 Answers 4

2

I use these two defines in my wp-config.php file:

define('WP_HOME', 'http://dev.mysite.com');
define('WP_SITEURL', 'http://dev.mysite.com');

Mind the trailing slash (or rather, the lack thereof), else you'll get permalink bugs.

Also note that they'll actually get used as is if you insert images and so forth when adding content.

1

Likely this will make more sense with JS, but that is not my area.

In PHP you can buffer output between two hooks (for example from init to late wp_footer), replace links and echo.

Also it can be very convenient to use hosts to redirect real domain to IP of test server.

1
  • Filtering buffer output is probably the best way to do this. With Javascript you might forget some cases (links, images, maybe even paths to external resources, ... all need to be replaced). The only downside is that you could also convert the domain name if it appears it your content, but that is a minor issue.
    – Jan Fabry
    Feb 9, 2011 at 15:06
1

This is not the direct answer to your need, but if you were going to switch subdomains, you should have created the links, images, etc. without the domain in them. You should have added a filter to remove the domain. Then you would have root absolute links beginning with '/' not 'http://www.domain.com/'.

Here's a start on how to make permalink root relative.

But you will need to also run this function when you save your content (via a hook) in the administration, so you can strip the absolute links and make them root relative.

1

Indeed this is not as I would expect. I am used to doing this statically. With that technique, aside from changing the DEFINEs in wp-config.php you'll have to search/replace the entries in the database. To do this, just take a MySQL dump and run the following script with something like script.sh dump.sql > new_dump.sql. Load newdump.sql into the DB and you should be good to go.

#!/bin/sed -f
s/'\([^\\']*\)old.domain.com\([^\\']*\)'/'\1new.domain.com\2'/g

You could try using the above regex in a preg_replace() to do this on the fly, but you might run into issues.

1
  • 1
    Your answer makes you seem older than your profile picture would indicate. Using sed on a DB dump is deliciously Old School ... I love it! Feb 9, 2011 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.