5

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!

6

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.

| improve this answer | |
  • Thanks. For now this code will work. I don't want to touch the WordPress core; but if the mentioned patched will be merged, could I just load another (or multiple) language files without huge performance hits? The language switcher of course appears on every page. – luke Jun 15 '15 at 14:16
  • @luke The performance hit might be less hard, but it will still be noticeable. – fuxia Jun 15 '15 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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