xili-language : a full featured installation with hooks…

[NOTE: ] This post wil be soon updated for latest version of the plugin !
In the first episode, was described a standard installation and integration of xili-language in a blog or cms with multilingual content (posts in different languages and links between them).

I a previous memo, we discuss about the features of plugins versus functions in functions.php file of the theme’s folder.

In xili-language plugin, it was decided and designed that core features are in the plugin and personalized features will be in the theme. It is possible because the plugin provides some hooks to enrich the plugin. The advantages are that themes can continue to be well designed in conformance of the look and the data-design.
It is far better than modifiying the plugin itself (with the problems of poor maintenance when upgrading by instance the plugin).

Why not create a admin settings UI ?
Because it is heavy in coding for poor performances – it is better to add few lines in functions.php than thousand of lines in plugin… (and with its cohort of adverse effects)

And the end of the article, a list of hooks will be shortly commented but now a detailled example about the template tag « xili_language_list ».

This tag is used in widget but can also be used in theme design by webmaster (in header, footer or elsewhere outside the loop).

In the default use, this tag xili_language_list() display the list of available languages and link them to the home (and show the most recent posts in the clicked language).

In this example, three things will be done : a flag will be added at each line, the class to provide info when the line is selected and a sort to choose the first top languages in the displayed list.

First step :

function my_xili_language_list($before = '<li>', $after ='</li>') {
}

Create the line to attach this function to the hook ‘xili_language_list’ (by « commenting » this line with // at start, default behavior is restored.

add_action('xili_language_list','my_xili_language_list',10,3); /* activate my_xili... instead the default in plugin */

Second step :

Don’t forget to add a series of flags in the theme’s images folder (subfolder = flags) with appropriate names.

flags in theme's images folder

flags in theme's images folder

Third step :

copy this script inside the function :

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
		$currenturl = get_bloginfo('siteurl').'/?';
		$a = "";
		$lines = array();
		$lineorder = array('fr_fr'=>1,'en_us'=>2); /* line to modify where to declare the top languages */
		foreach ($listlanguages as $language) {
			if ($before=='<li>') {
				if (the_curlang() == $language->slug) { 
					$beforee = '<li class="current-cat" >';
				} else {
					$beforee ='<li>';
				}
			}
			if (isset($lineorder[$language->slug])) { 
				$key = $lineorder[$language->slug]; /* set line sort */
			} else {
				$key = 1000 + $language->term_id; /* if array is empty */
			} 
			$lines[$key] = $beforee ."<a href='".$currenturl.QUETAG."=".$language->slug."' title='".__('Posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /> ". __('in '.$language->description,THEME_TEXTDOMAIN) ."</a>".$after;
 
		}
		ksort($lines); /*sort lines by key */
		foreach ($lines as $line) {
		 	$a .= $line;
		}
echo $a;

Some comments :

First lines set the vars and keep in db the array of languages, set the order list for sorting it.
After the loop where each line of the list are defined (and placed).
List is sorted and displayed…

Because the languages order can differ where the list is displayed, the array is here in functions.php and not yet in admin UI.

Fourth step :

Modify the line #107 containing the array $lineorder to set your languages series in order (by instance the first 2 tops if the site has more than 2 languages.)

103
$lineorder = array('fr_fr'=>1,'en_us'=>2); /* line to modify where to declare the top languages */

The final result is :

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function my_xili_language_list($before = '<li>', $after ='</li>') {
 
$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
		$currenturl = get_bloginfo('siteurl').'/?';
		$a = "";
		$lines = array();
		$lineorder = array('fr_fr'=>1,'en_us'=>2); /* line to modify where to declare the top languages */
		foreach ($listlanguages as $language) {
			if ($before=='<li>') {
				if (the_curlang() == $language->slug) { 
					$beforee = '<li class="current-cat" >';
				} else {
					$beforee ='<li>';
				}
			}
			if (isset($lineorder[$language->slug])) { 
				$key = $lineorder[$language->slug]; /* set line sort */
			} else {
				$key = 1000 + $language->term_id; /* if array is empty */
			} 
			$lines[$key] = $beforee ."<a href='".$currenturl.QUETAG."=".$language->slug."' title='".__('Posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /> ". __('in '.$language->description,THEME_TEXTDOMAIN) ."</a>".$after;
 
		}
		ksort($lines); /*sort lines by key */
		foreach ($lines as $line) {
		 	$a .= $line;
		}
echo $a;
}
add_action('xili_language_list','my_xili_language_list',10,3); /* activate my_xili... instead the default in plugin */

That all… until after the next episode

The list of main hooks of xili-language (see php code for more infos about args) :

Very useful for CMS theme’s designers…

1) hooks for template tags :
xili_language_list : here described.
xili_post_language : the language of current post for the tag under the title in loop.
xiliml_the_other_posts : the list of linked posts for the tag under the title in loop.
xiliml_the_category : the tag which replace the tag the_category.
xiliml_langinsearchform : the tab which add radio-buttons (addable in search form)

2) hooks for other core plugin functions :
xiliml_cur_lang_head (see line 310…) : the rules to change the theme’s language.
xiliml_cat_language : the rules for linking and displaying categories in sidebar menu.
… other described in plugin’s php code after lines 380.

Ce contenu a été publié dans Experts corner, xili-language, avec comme mot(s)-clé(s) , , , , , . Vous pouvez le mettre en favoris avec ce permalien.