Flying memes

Translating wp-members with WPML

I was asked to create a website where some content can be viewed only by registered users. I decided to go for WordPress and I choose wp-members plugin to enable member-only content as well as custom login and register pages.

Everything was going pretty well when I discovered that wp-members store in the wp_options table some labels and sentences, such as fields labels and all the templates of the emails to be sent to the user. This behavior is not compatible with WPML and results in the same label or sentence rendered without caring of the current user language.
Luckily there’s an easy workaround, we can create an hook that intercepts the request of such labels and substitue them when necessary.

To begin we have to create a filter call to be activated upon request of these properties, that’s easy because wp-member prefixes all of its properties with ‘wpmembers_’:

	$db_options = get_alloptions();

	foreach($db_options as $name => $value){
		if(strpos($name, 'wpmembers_') === 0){
			add_filter('option_' . $name, 'i18n_wpmembers');

Now we can define a ‘i18n_wpmembers’ function which determinate what property wp-members is looking for and acts by injecting translations taken from other properties, using name conventions:

function i18n_wpmembers($value){
	global $db_options;
	$language_code = defined('ICL_LANGUAGE_CODE') ? constant('ICL_LANGUAGE_CODE') : substr(get_bloginfo("language"), 0,2);

        // load translations if available (ex: it_wpmembers_options) 
	$translations = get_option($language_code . '_wpmembers_options');
	$current_option = str_replace('option_','',current_filter());

		case 'wpmembers_fields':
			$elements_to_return = array();
			foreach($value as $i => $field){
				$new_field = $field;
				$label = $new_field[2];
				$translated_opt = $translations ? $translations[$current_option . '_' . $label] : false;
                                // inject the translation into the wp-members structured property
				$new_field[1] = ($translated_opt ? $translated_opt : $field[1]);
				$elements_to_return[] = $new_field;
			return $elements_to_return;
                // continue with all the other wp-members properties

			return $value;

Now all we have to do is create an extra admin page which let create the translated properties, we can achieve this following the theme options guidelines.

Hope it might be useful!

Tags: , ,