I have a WordPress site in a temporary location, and the site URL is an IP address followed by slash, tilde and the short site name. I also have a number of internal links in the pages that assume linking from the root - <a href="/services"> - for example. Now these links do not work in the testing environment because of the shared IP.

Does anyone know what I can do to make my internal links work, no matter where the site lives?

UPDATE: I'm trying to update links (and the SRC of uploaded images) in the page content, not in a template file.

ANOTHER UPDATE: I think I'm going to just go the manual route and drop in absolute URLs.

  • Is this on a testing location that you control? Would it be possible to move it to a different port number on that shared IP? I always develop and test in that way, so the paths remain the same (it is indeed otherwise very difficult to test). – Jan Fabry Nov 9 '10 at 20:47
  • @Jan Fabry - I cannot control the port on the shared IP. – webcodeslinger Nov 9 '10 at 22:07
  • This is how I set up accounts that don't have a domain attached, and it sucks. A lot of plugins don't play well with this schema, because they often assume that the file path root is domain.com/ rather than ip/~user/ – Dan Gayle Nov 9 '10 at 22:26

You can wrap the links in echo home_url($path) to have them printed relative to the site. So your link would be coded as:

<a href="<?php echo home_url('/services')?>">
  • Thanks - however, when I paste this into the HTML tab of my page (not the Visual tab), it turns the symbols into "&gt;" etc. – webcodeslinger Nov 9 '10 at 20:29
  • 2
    @webcodeslinger: This solution works for files that are parsed as PHP, like template pages. It was not clear from your question that you were referring to links in the page and post content, maybe you should add that. – Jan Fabry Nov 9 '10 at 20:45

Add a shortcode that refers the site url like this in your functions.php, or use a plugin like this one: http://wordpress.org/extend/plugins/peters-blog-url-shortcodes/

Then your links can just be written like this: <a href="[blogurl]/services"> and they will still work when you move the site to a different server.


What you ask for is quite similar to what users experience when they move their site - they need to update links in all content to make changes. This is normally done by executing a SQL-query on the database to change all posts at once (Reference).

Keep in mind that this constantly changes all links. This is probably not what you want to.

For a temporary change, I suggest to buffer wordpress' output and then do either a best-guessed replace with preg_replace_all() or preg_replace_callback() and some regex or to do it safe by using fluentDOM and changing all links with ease by using it with the output buffer.

If this is for testing only, I would do the second, temporary method. This will actually prevent that the original content got changed, so this is on viewing the frontend of the blog only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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