Hooks in xili-language: example with languages list
In the first episode and others, 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).
What will we do ?
This tag is used in lastest « multiple » 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, a specific class to provide info when the line is selected and different types of lists will be set (one for header use without name and one for sidebar itself…
First step :
function my_xili_language_list($before = '<li>', $after ='</li>',$theoption) {
} |
function my_xili_language_list($before = '<li>', $after ='</li>',$theoption) {
}
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 */ |
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
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
129
130
131
132
133
134
135
136
137
138
139
140
| global $wp_query;
if ($theoption == 'top') {
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
$a ='';
$currenturl = get_bloginfo('siteurl').'/?';
foreach ($listlanguages as $language) { //QUETAG
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
$a .= $before ."<a href='".$currenturl."hlang"."=".$language->slug."' title='".__('Latest posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /></a>".$after;
}
}
echo $a;
} else { /* the current list in sidebar with category sub selection*/
if (is_category()) {
$catcur = xiliml_get_category_link();
$currenturl = $catcur.'&';
} else {
$currenturl = get_bloginfo('siteurl').'/?';
}
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
foreach ($listlanguages as $language) {
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
if ($before=='<li>') {
if (the_curlang() == $language->slug) {
$beforee = '<li class="current-cat" >';
} else {
$beforee ='<li>';
}
}
$a .= $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;
}
}
if (is_category()) {
$currenturl = xiliml_get_category_link();
$a .= $before."<a href='".$currenturl."' title='".__('Posts of current category in all languages',THEME_TEXTDOMAIN)."' >"." <img src='".get_bloginfo('template_directory')."/images/flags/www.png' alt='' /> ".__('in all languages',THEME_TEXTDOMAIN)."</a>".$after;
}
echo $a;
} |
global $wp_query;
if ($theoption == 'top') {
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
$a ='';
$currenturl = get_bloginfo('siteurl').'/?';
foreach ($listlanguages as $language) { //QUETAG
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
$a .= $before ."<a href='".$currenturl."hlang"."=".$language->slug."' title='".__('Latest posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /></a>".$after;
}
}
echo $a;
} else { /* the current list in sidebar with category sub selection*/
if (is_category()) {
$catcur = xiliml_get_category_link();
$currenturl = $catcur.'&';
} else {
$currenturl = get_bloginfo('siteurl').'/?';
}
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
foreach ($listlanguages as $language) {
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
if ($before=='<li>') {
if (the_curlang() == $language->slug) {
$beforee = '<li class="current-cat" >';
} else {
$beforee ='<li>';
}
}
$a .= $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;
}
}
if (is_category()) {
$currenturl = xiliml_get_category_link();
$a .= $before."<a href='".$currenturl."' title='".__('Posts of current category in all languages',THEME_TEXTDOMAIN)."' >"." <img src='".get_bloginfo('template_directory')."/images/flags/www.png' alt='' /> ".__('in all languages',THEME_TEXTDOMAIN)."</a>".$after;
}
echo $a;
}
Some comments :
First lines set the vars and keep in db the array of languages
After the loop where each line of the list are defined (and placed).
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
131
132
133
134
135
136
137
138
139
140
141
| function my_xili_language_list($before = '<li>', $after ='</li>',$theoption) {
global $wp_query;
if ($theoption == 'top') { /* usable in type item in new multiple widget */
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
$a ='';
$currenturl = get_bloginfo('siteurl').'/?';
foreach ($listlanguages as $language) { //QUETAG
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
$a .= $before ."<a href='".$currenturl."hlang"."=".$language->slug."' title='".__('Latest posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /></a>".$after;
}
}
echo $a;
} else { /* the current list in sidebar with category sub selection*/
if (is_category()) {
$catcur = xiliml_get_category_link();
$currenturl = $catcur.'&';
} else {
$currenturl = get_bloginfo('siteurl').'/?';
}
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
foreach ($listlanguages as $language) {
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
if ($before=='<li>') {
if (the_curlang() == $language->slug) {
$beforee = '<li class="current-cat" >';
} else {
$beforee ='<li>';
}
}
$a .= $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;
}
}
if (is_category()) {
$currenturl = xiliml_get_category_link();
$a .= $before."<a href='".$currenturl."' title='".__('Posts of current category in all languages',THEME_TEXTDOMAIN)."' >"." <img src='".get_bloginfo('template_directory')."/images/flags/www.png' alt='' /> ".__('in all languages',THEME_TEXTDOMAIN)."</a>".$after;
}
echo $a;
}
}
add_action('xili_language_list','my_xili_language_list',10,3); |
function my_xili_language_list($before = '<li>', $after ='</li>',$theoption) {
global $wp_query;
if ($theoption == 'top') { /* usable in type item in new multiple widget */
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
$a ='';
$currenturl = get_bloginfo('siteurl').'/?';
foreach ($listlanguages as $language) { //QUETAG
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
$a .= $before ."<a href='".$currenturl."hlang"."=".$language->slug."' title='".__('Latest posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /></a>".$after;
}
}
echo $a;
} else { /* the current list in sidebar with category sub selection*/
if (is_category()) {
$catcur = xiliml_get_category_link();
$currenturl = $catcur.'&';
} else {
$currenturl = get_bloginfo('siteurl').'/?';
}
$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');
foreach ($listlanguages as $language) {
if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
if ($before=='<li>') {
if (the_curlang() == $language->slug) {
$beforee = '<li class="current-cat" >';
} else {
$beforee ='<li>';
}
}
$a .= $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;
}
}
if (is_category()) {
$currenturl = xiliml_get_category_link();
$a .= $before."<a href='".$currenturl."' title='".__('Posts of current category in all languages',THEME_TEXTDOMAIN)."' >"." <img src='".get_bloginfo('template_directory')."/images/flags/www.png' alt='' /> ".__('in all languages',THEME_TEXTDOMAIN)."</a>".$after;
}
echo $a;
}
}
add_action('xili_language_list','my_xili_language_list',10,3);
That all… until 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 455…) : 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 1800 and resumed here.
Ce contenu a été publié dans
Experts corner,
xili-language, avec comme mot(s)-clé(s)
hook,
multilingual,
plugin,
widget. Vous pouvez le mettre en favoris avec
ce permalien.