Quantcast
Channel: polylang – WordPress.org Forums
Viewing all articles
Browse latest Browse all 11471

Elmo Allen on "[Plugin: Polylang] Multiple language switchers not working because of hard-coded id"

$
0
0

Multiple language switchers on a same page using PLL_Widget_Languages are not working, because all the widgets are created with an identical id attribute and the JavaScript hence only reacts to the first element clicks. The code behind seems to almost support using custom id set with the args array, but not quite. More elaborately:

I tried to create a second widget with the following:

the_widget('PLL_Widget_Languages', array(
  'id' => 'lang_choice_custom_id',
  'name' => 'lang_choice_custom_id'
));

And the PLL_Walker_Dropdown eventually creates the <select> with correct ID:

<select name="lang_choice_custom_name"  id="lang_choice_custom_id">
  <option value="en" selected="selected">English</option>
  ...
</select>

But the problem is, the JavaScript snippet the widget creates uses a hard-coded id parameter (include/widget-languages.php:54):

var d = document.getElementById('lang_choice');

This is easily corrected, if you just add a line before the JavaScript:

$id = $instance['id'];

And then change that to the JavaScript line with embedded PHP variable:

var d = document.getElementById('{$id}');

https://wordpress.org/plugins/polylang/


Viewing all articles
Browse latest Browse all 11471

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>