In my web pages I want to use relative links instead of absolute links.

However, pages do not allow php code inside them, so i cannot do to from a url in a relative manner.

How does one get relative urls inside wordpress pages so that changing domain names wouldnt affect the links?

8 Answers 8

$my_url = 'my/relative/url.php';
echo site_url($my_url);

site_url() when used by itself will return the absolute path to your blog. But, if you add an argument to it, as per my example above, it will prepend the absolute path to your path. Just make sure your URL doesn't contain a leading slash (eg: /this/may/not/work).

Finally, if you happen to have your wordpress installed in your server's root, you can use a server-relative path (this uses the leading slash to indicate starting at the server root). So if your blog is installed at http://www.me.com/blog then you can access your relative links safely with /blog/my_link.php.


WordPress has a built-in function for removing protocol and domain from absolute URLs, wp_make_link_relative, located in /wp-includes/formatting.php:

function wp_make_link_relative( $link ) {
    return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );

To apply this function to (e.g.) permalinks, simply add a filter, as such:

add_filter( 'the_permalink', 'wp_make_link_relative' );

Deluxe Blog Tips shows how to apply this to various types of links, while making sure feed and sitemap links aren't affected:

add_action( 'template_redirect', 'rw_relative_urls' );
function rw_relative_urls() {
    // Don't do anything if:
    // - In feed
    // - In sitemap by WordPress SEO plugin
    if ( is_feed() || get_query_var( 'sitemap' ) )
    $filters = array(
    foreach ( $filters as $filter )
        add_filter( $filter, 'wp_make_link_relative' );

two ways to do that:

  • this only talks about permalinks, not about how to use relative urls instead of absolute
    – pdeva
    Commented Feb 23, 2011 at 0:53
  • 2
    Scroll down on that page or better yet codex.wordpress.org/…
    – Bainternet
    Commented Feb 23, 2011 at 1:07

I used to use shortcode with get_bloginfo for relative paths inside actual post content. I was going to turn it into a full blow plugin but I could have sworn the folks at automatic released one which I can no longer find. Here's is a simple example of how it work as I can't find mine atm since I don't use it anymore, as per Bainternet post, maybe the folks at automatic realized this too..

A simple shortcode function that will return your base url:

function my_relative_post_image_link() {
$linky = get_bloginfo('wpurl');
echo $linky;
add_shortcode('posty', 'my_relative_post_image_link');

If your site is example.com then putting the shortcode [posty] in your content will return example.com as text.

To make actual use of this you will need to customize the function using more parameters from get_blogfino and wrap it in the necessary html tags http://codex.wordpress.org/Function_Reference/get_bloginfo


use the default linking option in wordpress, remove the domain name and start it with /, for example





This function should be put into your theme's functions.php. It will make all permalinks relative.

// make root relative
function make_href_root_relative($input) {
    return preg_replace('!http(s)?://' . $_SERVER['SERVER_NAME'] . '/!', '/', $input);
function root_relative_permalinks($input) {
    return make_href_root_relative($input);
add_filter( 'the_permalink', 'root_relative_permalinks' );

Images in content should use absolute links IF those images go out to feed readers. If images use relative links the feed readers try to load the images from their own domains. There is a small performance hit in using relative links to images too. It doesn't appear to make any difference with the relative permalinks.

On some servers, you may need to add base href with your site root to ensure links work as expected.


I think that there is an Absolute Relative Links plugin that will replace all the links in your pages with relative links. It works quite beautifully meaning that you don't have to go back and fix any old links that are still absolute.


This tutorial shows you a basic way to change post permalink to relative. I also improved that method to make all links in WordPress relative. The final code is here.

  • 3
    Naked links don’t help. Add at least an explanation.
    – fuxia
    Commented Apr 24, 2011 at 8:44

Your Answer

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

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