• I too was looking for a way to do what jderosa3 was wanting to do in this post: http://wordpress.org/support/topic/using-relative-instead-of-absolute-links-for-images and dismayed to read the responses saying “If you really want to use relative links or image paths, then don’t use WordPress”.

    It would be bad practice in any other development environment for any components to depend upon the absolute path of other components. The same is equally true for websites. Moving a site between development servers and live servers is common enough to make having to run scripts on the database a little bit of a pain. Wouldn’t it be nice for the site to work irrespective of its home url?

    What I wanted was a way to do links that were relative to the site (not dependent on the site’s url) but not relative to the page so if the page is moved they still work.

    I have found a reasonably simple way and thought I would share it with others that might find this useful:

    Install this plugin: http://wordpress.org/plugins/php-code-for-posts/
    So you can create php snippets to put in pages.

    Create a snippet for

    <?php echo get_site_url(); ?>

    Then anywhere you want a link or a src you can use [php snippet=1] for the site’s url like these examples:

    <a href="[php snippet=1]/main-pages/another-page">Another Page</a>
    
    <img  src="[php snippet=1]/images/logo.jpg" width="300" height="100" />

    Pages can be moved and the links in them don’t break.
    The site can be moved and the links in its pages don’t break.

    I hope others find that useful.
    Thanks to the.missing.code for making the plugin that makes this possible. An essential plugin for every site I make now. If only it was a standard feature.

  • The topic ‘Relative vs Absolute Links: the best of both worlds’ is closed to new replies.