rss
4

WordPress Network (multisite) multiple language problems

  • Sumo

WordPress Network is a great way to handle multi-language websites with the same user base, same theme and same plugins. But!

There are a couple of very uncomfortable things I would like WordPress team to fix.

1. WPLANG global variable, that we used to define in wp-config.php is no longer used by the system! Why? Because of this function in wp-includes/i10n.php

function get_locale() {
	global $locale;

	if ( isset( $locale ) )
		return apply_filters( 'locale', $locale );

	// WPLANG is defined in wp-config.
	if ( defined( 'WPLANG' ) )
		$locale = WPLANG;

	// If multisite, check options.
	if ( is_multisite() ) {
		// Don't check blog option when installing.
		if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) )
			$ms_locale = get_site_option('WPLANG');

		if ( $ms_locale !== false )
			$locale = $ms_locale;
	}

	if ( empty( $locale ) )
		$locale = 'en_US';

	return apply_filters( 'locale', $locale );
}

This function assumes, that if you have multiple websites and one wp-config.php, then this variable should be loaded from the database under totally different name $locale.  Even more it does not use WPLANG for loading translation file anymore. But this is not documented. And I lost couple of hours trying to figure out, why WordPress loads wrong translation file, with this code that I had in my wp-config.php.

if( $_SERVER['HTTP_HOST'] == 'ru.somewebsite.com' ) {
    define('WPLANG', 'ru_RU');
}elseif( $_SERVER['HTTP_HOST'] == 'am.somewebsite.com' ) {
    define('WPLANG', 'hy_AM');
}else{
    define('WPLANG', 'en_US');
}

So be careful and remember that after installing WordPress multisite, translation files are loaded according to  the site meta WPLANG, which is saved in DB. And don’t try to update this value from the admin panel, because it is buggy and does not work. Just go to the database with phpmyadmin and change the value there.

Now I use the above code in wp-config.php to define WPLANG variable and use it in my theme.

 

 2. Share same library (uploads folder)

For a multi-language website you probably would like to have a shared library of images and uploaded stuff (wp-content/uploads folder). In a multisite setup of database, each site has its own posts and postmeta tables, and they are  prefixed with the site number, like wp_2_posts and wp_3_posts. And accordingly they are using different sub-folders in the wp-content/uploads; wp-content/uploads/sites/2/ and wp-content/uploads/sites/3/.

By the way, images and all uploaded stuff is kept in wp_posts table, in case you did not know about it. So the question is how to have one shared library for all websites, so that people can use the image that they already uploaded for one language, and avoid multiple uploads of the same image on server?

Good people even wrote a plugin for this purpose, but it does not work properly.

For this issue I have a partial solution. Which means I only managed to physically point all three different upload folders to the same one. How? Simple and bold solution.
I have removed wp-content/uploads/sites/2 and wp-content/uploads/sites/3 folders and made  links instead of folders

uplodas# ln -s ../../ 2
uplodas# ln -s ../../ 3

Later I found a function in wp-includes/functions.php that is responsible for uploads folder and an interesting code inside :)

function wp_upload_dir( $time = null ) {
	...
	// If multisite (and if not the main site in a post-MU network)
	if ( is_multisite() && ! ( is_main_site() && defined( 'MULTISITE' ) ) ) {
		if ( ! get_site_option( 'ms_files_rewriting' ) ) {
			if ( defined( 'MULTISITE' ) )
				$ms_dir = '/sites/' . get_current_blog_id();
			else
				$ms_dir = '/' . get_current_blog_id();
			...

The good news, is that this function uses apply_filters( ‘upload_dir’, …), which means you can  change the folder path  by hooking this filter. Thank you WordPress for that, but I prefer my simple solution.

So the problem with path you can solve either with the trick I described above, or with upload_dir hook, but still there is a big issue with database tables, for which I am still looking for a solution.
Now users upload images to the same folder, but still they cannot use the same image in all websites, which is very uncomfortable.

The only thing that comes to my mind, is to manually synchronize all wp_N_posts and wp_N_postmeta tables with the attachment_fields_to_save hook. Still I did not implement it yet, but eventually I will come to this solution, if nothing smarter comes to my mind.

So the question is open for discussion, if you have suggestions or comments, please contribute.

About the Author

Edgar Marukyan is an expert of website development and performance optimization, who maintains and consults clients with high load traffic websites.. If you need an assistance, feel free to drop me a line.         Website - Twitter - Facebook

Comments (4)

Trackback URL | Comments RSS Feed

  1. Արտակ says:

    very useful and interesting article , thank you very mach !

  2. al says:

    Very informative post. Saved me couple hours. Thanks!

  3. Ginnie says:

    Hi Edgar! Great investigation you made. I wonder if you found some solution for “uploads_dir hook”, to make all files available for all the sub-sites…?

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.