WordPress: Customize Polylang Language Switcher

WordPress: Customize Polylang Language Switcher

A default language switcher for Polylang it’s a bit out of date. Below are the customized Polylang Language Switcher.

Customized Polylang Language Switcher
Customized Polylang Language Switcher

Below are the codes used for the customization.

 


include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

// check if polylang exist & enabled
if ( is_plugin_active( 'polylang/polylang.php' ) ) {
  	//plugin is activated
	add_filter('pll_the_languages', 'my_dropdown', 10, 2);
	function my_dropdown($output, $args) {
	    $translations = pll_the_languages(array('raw'=>1));
	    $output = '';
	    $output .= '<div class="btn-group" role="group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      '.$args['title'].'
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">';

	    foreach ($translations as $key => $value) {
	    	$output .= '<li><a href="'.$value['url'].'"><img src="'.$value['flag'].'" alt="'.$value['slug'].'"> ' .$value['name'].'</a></li>';
	    }

	    $output .= '</ul></div>';
	    return $output;
	}
} 

References:

  1. Check if a plugin is active or not
  2. Polylang Functions Reference

One thought on “WordPress: Customize Polylang Language Switcher

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 4 =