I’ve been using the WPML plugin on a few sites recently to turn WordPress into a very powerful multilingual CMS, with great success. I really have to applaud these guys for building a very useful plugin. On the to-do list is integrating their plug-in with WordPress’ built-in widgets. A few people have mentioned getting stuck when trying to use the Links feature of WordPress, which does not currently work with WPML.
To fix this issue, I’ve thrown together a quick widget that acts as a wrapper for WordPress’ built-in widget, but lets you set a different link category for each language you have setup using WPML. I hope you find this useful.
To install, copy and paste this into your theme’s function.php file or insert code into a file in the plugins directory.
* NOTE – This code may be out of date. Please try commenter Loic’s solution here if the code below does not work properly.
* NOTE – Commenter Uli has also improved this plugin and extended it in a similar way to translate RSS Feeds. Visit barcelonabudget.es for more info.
add_action('widgets_init', array('LinkTranslationWidget','register_widget')); class LinkTranslationWidget extends WP_Widget { function LinkTranslationWidget() { /* Widget settings. */ $widget_ops = array( 'classname' => 'LinkTranslationWidget', 'description' => __('This widget allows you to set different link categories to display based on a language setting.', $this->plugin_name) ); /* Widget control settings. */ /* Create the widget. */ $this->WP_Widget( 'LinkTranslationWidget', __('WPML Links'), $widget_ops ); } function register_widget() { register_widget('LinkTranslationWidget'); } function widget( $args, $instance ) { global $sitepress; if(class_exists('WP_Widget_Links')) { $link_widget = new WP_Widget_Links(); if(isset($sitepress)) { $lang = $sitepress->get_current_language(); $instance['category'] = $instance[$lang.'_category']; $link_widget->widget($args, $instance); } } } function update($new_instance, $old_instance) { $new_instance = (array) $new_instance; $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0); foreach ( $instance as $field => $val ) { if ( isset($new_instance[$field]) ) $instance[$field] = 1; } $langs = $this->GetLangs(); foreach($langs as $lang=>$lang_id) { $instance[$lang.'_category'] = intval($new_instance[$lang.'_category']); } return $instance; } function form($instance) { $settings = $this->GetSettings(); $langs = $this->GetLangs(); $instance = wp_parse_args( (array) $instance, $settings ); $link_cats = get_terms( 'link_category'); foreach($langs as $lang=>$lang_id) { echo ' <label for="'. $this->get_field_id($lang.'_category').'">'. __('Select Link Category') . ' (' .$lang.'): </label>'; echo ' <select id="'. $this->get_field_id($lang.'_category').'" name="'. $this->get_field_name($lang.'_category').'">'; foreach ( $link_cats as $link_cat ) { echo ' <option value="' . intval($link_cat->term_id) . '">term_id == $instance[$lang.'_category'] ? ' selected="selected"' : '' ) . '>' . $link_cat->name . "</option> \n"; } echo '</select> '; } ?> <input class="checkbox" type="checkbox" /> id="<!--?php echo $this--->get_field_id('images'); ?>" name="<!--?php echo $this--->get_field_name('images'); ?>" /> <label for="<?php echo $this->get_field_id('images'); ?>"><!--?php _e('Show Link Image'); ?--></label> <input class="checkbox" type="checkbox" /> id="<!--?php echo $this--->get_field_id('name'); ?>" name="<!--?php echo $this--->get_field_name('name'); ?>" /> <label for="<?php echo $this->get_field_id('name'); ?>"><!--?php _e('Show Link Name'); ?--></label> <input class="checkbox" type="checkbox" /> id="<!--?php echo $this--->get_field_id('description'); ?>" name="<!--?php echo $this--->get_field_name('description'); ?>" /> <label for="<?php echo $this->get_field_id('description'); ?>"><!--?php _e('Show Link Description'); ?--></label> <input class="checkbox" type="checkbox" /> id="<!--?php echo $this--->get_field_id('rating'); ?>" name="<!--?php echo $this--->get_field_name('rating'); ?>" /> <label for="<?php echo $this->get_field_id('rating'); ?>"><!--?php _e('Show Link Rating'); ?--></label> <!--?php } function GetLangs() { global $sitepress_settings; $langs = $sitepress_settings['default_categories']; return $langs; } function GetSettings() { $settings = array(); $settings['images'] = true; $settings['name'] = true; $settings['description'] = false; $settings['rating'] = false; $langs = $this--->GetLangs(); foreach($langs as $lang=>$lang_id) { $settings[$lang.'_category'] = ''; } return $settings; } }

23 Comments »
Hey thanks for the sharing,
it seems easy to implement, but i have a little question:
where can i change the link category or the translations of the blogroll?
Could you post some help about how to use this once the code is copied in functions.php?
Thanks a lot !
When I try the the widget it displays the links of both languages at the same time. Not just the links for the selected language as I expected.
Is it supposed to do this?
Best Regards,
Chris
It works fine now. Please delete!
Chris
Awesome! Exactly what I needed, works perfectly. Thank you!
Thanks! Works like a charm.
I think the latest WPML has broken this widget. I’m getting this error displayed above my links in the sidebar:
Warning: Invalid object kind in wp-content/plugins/sitepress-multilingual-cms/inc/template-functions.php on line 181
My default English page is fine, but the error appears on my French page and instead of displaying just the assigned category from the widget it shows all the links in both languages.
This wrapper is really neat. Tried it out and it works like a charm. Very nicely done.
I’m trying to use this widget with Spanish as my default language, but when I switch to english in the front end, it displays ALL the links categories instead of the one that I’m assigning as the english version… any suggestions?
Dear Dan,
thanks for the above solution it helped me in translating the links however, I found one issue in implementing it which is:
when I visit the second language the code place all the translated categories (E.g. English and Arabic).
Kindly if you can help me in solving this issue please?
I have links and it works when i have selected default language. When i set another language it show me all links category (default language category and selected language category :/)
Hello, Will this only work for sidebar widgets or can it also be used for a link page template? (For example, I have my links generated on page by a link page template).
What modifications would be needed to make it work on a page template?
Thanks
Hi!
Sorry for the stupid question but I am not a programmer, or pro website builder still try to manage some WP sites. Where should I copy exactly your coding? Anywhere in function.php? If I would like to create a new file how will it be connected to the site? What name should I give to it.
Thanx in advance: Viktor
Howdy
I have a small problem with your widget. On the default language, it works fine, it display only the default language links, named “Ce mai citesc”. But on the secondary page, it shows both the default language links, and the secondary language links, which would be “What I read”. What should I do? Thank you
Sorry I haven’t responded to these requests. I have a feeling recent versions of WPML take care of this issue. If anyone still uses this plugin and wants me to have a look at it, please let me know.
Hi,
Like Victor, I am not a programmer and I don’t know where exactly to paste the file.
Hi there,
It seems that there’s still some issues between the WP links plugin and the current free WPML plugin. Thanks to your widget, it seems that everything is now working as expected, so Thank you !
I just had to hack a little bit into your code to fit my needs and make it mork with my version of WP.
Here is my updated version : http://pastebin.com/e3g32sdj
Happy blogging !
Thanks for sharing, I’ve added your code sample to the top of the post, but I’ve left the original in case that helps anyone.
Added to this… empty as menu option + empty check on base widget override. Otherwise this widget will show all link categories… instead of nothing if you don’t have a category with or without links…
Will publish the “augmented” version of this widget these days… on one of my websites and post a link here.
[...] primero es una modificación necesaria de una propuesta encontrada en la web y permite filtrar las listas de los enlaces externos según idioma seleccionado. Se llama WPML-Link [...]
[...] first plugin is a modification, contribution of a plugin we found here. This plugin allows to display filtered lists of external links based on display [...]
Hello Dan.
I posted here that I will return and leave you a message with the link where you can find the widget. Well, you will find two plugins… I did a second one that allows the same for the standard WordPress RSS Feeds.
My contributions fixes two little things… WPML-Link now maintains the configuration when you get back… and you can define empty as category which automatically supresses the output for the display language without proper link category.
The second widget handles all of this quite in the same way for RSS Feeds… it’s called WPML-RSS-Feed
You can find both plugins on barcelonabudget.es
Regards.
uli
Thanks for your contribution Uli. I’ll update the post to include your links.
Leave a comment