Relative instead of absolute links in pages?

I have my blog under blog.mydomain.com. I am using pages for some static content that needs to be accessed from another subdomain. So careers.mydomain.com will show WordPress content from blog.mydomain.com/careers.

My problem is that all the links on the page still reference blog.mydomain.com. Is there a way to ask WordPress to use relative links? Or a way to change the baseURL for pages?

Related posts

Leave a Reply

1 comment

  1. There is a filter, post_link, that permalinks pass through before being returned from get_permalink in wp-includes/link-template.php. You can use that filter to alter the links.

    However, beware that making all permalinks relative may have unintended consequences in certain contexts, e.g. you might not want relative links when is_feed() == true.

    You may find that the post_link filter doesn’t catch all of the URLs you need changed. In that case, you can try the home_url filter which has much broader effect.

    Another possibility (my favorite) is to use output buffering to relativize the links. (Example at bottom.)

    To be safe, you can change the links only when necessary. I don’t know how you’re getting the content from the actual blog to the static subdomain, but if you’re using HTTP requests you can add a query arg: ?rlz and then look for that in your theme/plugin.

    if ( isset($_GET['rlz']) ) {
        function my_ob_handler($HTML) {
            return str_replace( home_url('/'), '/', $HTML );
        }
        ob_start( 'my_ob_handler' );
    }