Widget to Translate Blog Link Categories with WPML
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 | 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 '<p><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) . '"'
. ( $link_cat->term_id == $instance[$lang.'_category'] ? ' selected="selected"' : '' )
. '>' . $link_cat->name . "</option>\n";
}
echo '</select></p>';
}
?>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> 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><br />
<input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> 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><br />
<input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> 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><br />
<input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> 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>
</p>
<?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;
}
} |

