• Hello,

    I am trying to modificate plugin to be at least a bit compatible with Polylang plugin.

    I am getting a strange behaviour when trying to get current locale when using filters in functions.php, or even if hardcoding it in plugin code. If I come and browse my site, the correct locale is not retrieved. If I change language, when the locale is set. If I change back to initial language, locale is also set well.

    Here are my functions:

    functions.php:
    
    function kcsite_alo_easymail_get_language(){
    
    	return pll_current_language('slug');     //doesn't always work
    	//return substr(get_locale(), 0, 2);     //doesn't always work
    	//return 'lt';                            //works
    }
    add_filter ( 'alo_easymail_multilang_get_language', 'kcsite_alo_easymail_get_language' );
    
    function custom_easymail_new_subscriber_is_added($subscriber, $user_id=false){
    
    	$lang = pll_current_language('slug');
    	$lang = (string)$lang;
    
    	global $wpdb;
        $wpdb->update($wpdb->prefix.'easymail_subscribers', array('lang' => $lang), array('ID' => $subscriber->ID));
    }
    add_action('alo_easymail_new_subscriber_added',  'custom_easymail_new_subscriber_is_added', 10, 2 );

    Am I doing it in the proper way, or did I miss something?

    https://www.ads-software.com/extend/plugins/alo-easymail/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author eventualo

    (@eventualo)

    I don’t know more about Polylang. I’ve installed and make a quick test.
    I added the following function:

    function echo_current_language() {
    	echo 'LANG = '.pll_current_language();
    }
    add_action( 'init', 'echo_current_language' ); 	// it does not print
    add_action( 'wp', 'echo_current_language' );	// it prints

    When init hook is fired maybe the polylang object has not been yet created. Instead, when wp hook is fired the language code is printed.
    So maybe also alo_easymail_multilang_get_language is called before polylang is ready. But it seems strange…
    Anyway, I found inside code that the current language code is stored in a cookie: $_COOKIE['wordpress_polylang']. A bit rude but works.
    For now it can works but I suggest that you search about this issue in Polylang forum or docs.

    The particularity of Polylang is that it offers two possibilities to set the language

    1. based on the content (as Xili language). In that case it is needed to wait for the query beeing parsed to set the language. So it is set in a ‘wp’ hook.

    2. based on the language code added to the url (as WPML and qtranslate). In that case, the language is set much sooner in the ‘setup_theme’ hook.

    The cookie is not a reliable way to get the language (in case the user switches from one language to another) as it is set in the action hooked to ‘wp’.

    Plugin Author eventualo

    (@eventualo)

    Ok, thanks Chouby for the explanation.
    I’ve updated the p4004 code and I’ve added a alo_easymail_multilang_get_all_languages filter.
    Now the subscriber language is properly stored because EasyMail uses to check it against available languages: before the languages array was empty, now it contains the PL languages and the subscriber language is stored.
    p4004 here you are the new code, let us know your feedback:

    function kcsite_alo_easymail_get_language( $lang, $detect_from_browser ) {
    
    	if ( function_exists('pll_current_language') )
    		$lang = pll_current_language('slug');
    	return $lang;
    }
    add_filter ( 'alo_easymail_multilang_get_language', 'kcsite_alo_easymail_get_language', 10, 2 );
    
    function kcsite_alo_easymail_multilang_get_all_languages( $langs, $fallback_by_users  ){
    
    	if ( function_exists('pll_the_languages') )
    	{
    		global $polylang;
    		if (isset($polylang))
    		{
    			$pl_languages = $polylang->get_languages_list();
    			if ( is_array($pl_languages) ) foreach( $pl_languages as $i =>$pl_lang )
    				$langs[] = $pl_lang->slug;
    		}
    	}
    	return $langs;
    }
    add_filter ( 'alo_easymail_multilang_get_all_languages', 'kcsite_alo_easymail_multilang_get_all_languages', 10, 2 );

    Thread Starter p4004

    (@p4004)

    Hi,

    Thanks a lot for your support. I added your code to functions.php file and the language is detected properly. I tried visiting subscription page and subscribing with all cookies cleared, also after browsing several pages on site. It works fine.

    Just to make sure: I only needed to add your code? I did not need to upgrade the plugin? ??

    There is one issue left: when confirmation link is generated, the language GET parameter is added, and the link gets broken. I assume this is because Polylang works not by GET parameter, but I may be completely wrong.

    What I did for now was commenting this line in alo-easymail_functions.php around line 3141:

    // last case: return th url with a "lang" var... maybe it could be useful...
    return add_query_arg( "lang", $lang, get_permalink( $post ) );

    After commenting it, the link became working and subscribers can confirm their subscription. But they are always directed to the page of main site’s language.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can't get current locale (trying to implement Polylang)’ is closed to new replies.