0

I'd been having trouble with the full domain name being written into the path of links and assets. Basically localhost:XXXX works fine, until I try to reach my site from a Virtual Box install of XP or in my local network, where the domain changes to computername.local:XXXX

I managed to solve this problem with a tip from http://www.deluxeblogtips.com/2012/06/relative-urls.html (code below)

The problem is that home_url which is referenced throughout my theme still returns the full domain, rather then just "/" as root. is there a way to filter home_url to do this? yes I could define('WP_HOME', '/' ); which will brute force the path, but causes other issues with feed readers and plugins - it also probably wont work with subdirectory installs.

thanks for your thoughts

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' );
}
    home_url($path = '', $scheme = null);
}

2 Answers 2

4

You need to set the WP_HOME and WP_SITEURL in wp-config.php in a smarter way. Like this:

<?php
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
// add the next line if you have a subdirectory install
define('WP_SITEURL', WP_HOME . '/path/to/wordpress');

This will solve your your issues with site URLs as they will be set dynamically on based on whatever the hostname is. This is the correct way to do this -- it's something I do on virtually ever WP install and it's even in the codex. Believe me, you don't really want relative urls.

6
  • What happens if the site is accessible by both HTTP and HTTPS? Feb 10, 2014 at 22:36
  • 1
    Look at how WP's is_ssl works, use a similar logic in wp-config.php. Feb 10, 2014 at 22:54
  • Thanks. What is the benefit of hard coding the protocol versus using a protocol relative URL? Feb 10, 2014 at 23:00
  • 1
    WP_HOME is used other places than just in links to stylesheets and such -- namely permalinks. You can try it, it just might be a little weird. Specifying a protocol relative WP content URL is usually okay, however. Feb 10, 2014 at 23:14
  • Thanks @chrisguitarguy. After messing around with define('WP_HOME', '/foo'); I realized that it breaks the TinyMCE editor, because the editor isn't smart enough to handle relative URLs to its javascript assets. Feb 11, 2014 at 19:17
0

home_url calls get_home_url which has the filter home_url:

return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );

see /wp-includes/link-template.php

4
  • Humm - I've done as much. Basically I revised get_home_url and put it in a functionality plugin, just to see.
    – orionrush
    Jun 24, 2012 at 9:28
  • I revised get_home_url and tried it in a functionality plugin, as wellas in the functions.php of child and parent. function tr_get_home_url( $blog_id = null, $path = '', $scheme = null ) { $orig_scheme = $scheme; $scheme = 'http'; $url = '/'; if ( !empty( $path ) && is_string( $path ) && strpos( $path, '..' ) === false ) $url .= '/'; return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); } With no effect––So I guess that this would mean that home_url is already being filtered elsewhere in the theme?
    – orionrush
    Jun 24, 2012 at 9:36
  • FYI this query is related to github.com/retlehs/roots/issues/490
    – orionrush
    Jun 24, 2012 at 13:18
  • @orionrush you're not filtering it correctly, you need to attach your filter function to home_url with add_filter. The apply_filter code I gave above is where the filter is actually applied in source.
    – Milo
    Jun 24, 2012 at 13:40

Your Answer

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

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