Remove WPML’s home_url filter

The WPML plugin filter’s home_url in order to provide a link to the home page specific to the language you are viewing. (So if you are reading a Spanish page home_url() will point you to http://example.com/es). I need to disable for a site that will not have language-specific homepages. So I went digging into the WPML plugin, which is essentially launched in a typical way:

global $sitepress;
$sitepress = new SitePress();

Then in the class’s constructor the offending filter is added:

Read More
add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );

With the class’s callback method being (for reference):

// filter for WP home_url function
function home_url( $url, $path, $orig_scheme, $blog_id )
{
    // exception for get_page_num_link and language_negotiation_type = 3
    if ( $this->settings[ 'language_negotiation_type' ] == 3 ) {
        $debug_backtrace = debug_backtrace();
        if ( !empty( $debug_backtrace[ 6 ] ) && $debug_backtrace[ 6 ][ 'function' ] == 'get_pagenum_link' )
            return $url;
    }

    remove_filter( 'home_url', array( $this, 'home_url' ), 1 );
    // only apply this for home url - not for posts or page permalinks since this filter is called there too
    if ( ( did_action( 'template_redirect' ) && rtrim( $url, '/' ) == rtrim( get_home_url(), '/' ) ) || $path == '/' ) {
        $url = $this->convert_url( $url );
    }
    add_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );

    return $url;
}

This seems like it should be a straight-forward case of remove_filter. However adding the following to functions.php (admittedly it should maybe be in a site-specific plugin) does not seem to remove the filter and the main logo (which uses the home_url() function is still pointing to the language-specific homepage).

function rar_remove_wpml_home_filter(){
    global $sitepress;
    remove_filter( 'home_url', array( $sitepress, 'home_url' ), 1, 4 );
}
add_action( 'plugins_loaded', 'rar_remove_wpml_home_filter' );

I’ve also tried running this on the init hook. It seems I must me missing something obvious.

Related posts

Leave a Reply

4 comments

  1. I found that it required removing 4 filters/actions to undo WPML’s home_url modification. Since I didn’t know what else this might break, I ended up changing my site’s logo to use site_url() which in my case was the same, but not changed by WPML.

    function disable_things(){
        remove_filter( 'page_link', array( $this, 'permalink_filter' ), 1, 2 );
        remove_action( 'wp_head', array( $this, 'front_end_js' ) );
        remove_filter( 'feed_link', array( $this, 'feed_link' ) );
        remove_filter( 'home_url', array( $this, 'home_url' ), 1, 4 );
    }
    add_action( 'wp_head', 'disable_things', 20 );
    
  2. I did it like this. Used WPML filter and returned default url rather than filtered one.

    function custom_wpml_home_url( $home_url, $url, $path, $orig_scheme, $blog_id ) {
        return $url;
    }
    add_filter( 'wpml_get_home_url', 'custom_wpml_home_url', 99, 5 );
    
  3. What about taking a different approach?

    Keep the filter and in WPML -> Languages, chose “Use directory for default language”.

    There you can specify a static file or create a “Root page”.

    This page won’t be connected to any language.
    It won’t even show the language switcher (unless you use a custom template and play with the code).