Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

7 Answers 7

up vote 5 down vote accepted
$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.

share|improve this answer

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.

share|improve this answer
3  
Naked links don’t help. Add at least an explanation. –  toscho Apr 24 '11 at 8:44
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Wyck Aug 19 '12 at 0:51

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' ) )
        return;
    $filters = array(
        'post_link',
        'post_type_link',
        'page_link',
        'attachment_link',
        'get_shortlink',
        'post_type_archive_link',
        'get_pagenum_link',
        'get_comments_pagenum_link',
        'term_link',
        'search_link',
        'day_link',
        'month_link',
        'year_link',
    );
    foreach ( $filters as $filter )
    {
        add_filter( $filter, 'wp_make_link_relative' );
    }
}
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

two ways to do that:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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