switch_to_blog(): Load textdomain

I am using the WordPress multisite feature with Multilingual Press Pro to create a multilingual site with linked posts.

In a network activated plugin the custom post types are registered. For translating the slug I use l10n:

'rewrite'   => array(
        'slug' => __( 'products', 'text-domain' ) . '/%product-category%',
        'with_front' => false
        )

This works fine. But, to link to the translated post on another site within the network, switch_to_blog() is used. As within this function no plugins are (re-)loaded, my translation files are also not loaded so the permalinks I retrieve will be wrong.

Is there any possibility to hook into switch_to_blog() to load another text domain?

Thanks!

1
1

You could hook into the action switch_blog. You get the new blog ID as the first argument here. But loading the complete translation files here is expensive, you also have to restore the old files after that.

WordPress does not use native gettext functions, but some custom code that is much slower. See #17268. The performance penalty for that would be huge.

Filter the URLs from the plugin instead, in this case mlp_linked_element_link, and use a hard-coded list of replacements like this:

add_filter( 'mlp_linked_element_link', function( $url, $site_id ) {

    if ( 1 === (int) $site_id )
        return str_replace( '/produkt/', '/product/', $url );

    if ( 2 === (int) $site_id )
        return str_replace( '/product/', '/produkt/', $url );

    return $url;

}, 10, 2 );

This is much faster. Permalinks and language file handling is still broken in multisite, you have to live with compromises.

Leave a Comment