3

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, '/' ) == 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.

1

4 Answers 4

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

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

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

0

You could use get_option( 'home' );

It will give you back blog’s home web address set in General Options.

Your Answer

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

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