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:

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, "https://wordpress.stackexchange.com/" ) == rtrim( get_home_url(), "https://wordpress.stackexchange.com/" ) ) || $path == "https://wordpress.stackexchange.com/" ) {
        $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.

4 Answers
4

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 );

Leave a Reply

Your email address will not be published. Required fields are marked *