• g100g

    (@g100g)


    Hi,
    I can’t use pll_current_language() function and pll_e() function in an AJAX call.

    I use the wordpress hook

    add_action('wp_ajax_myaction', 'myaction_function' );
    add_action('wp_ajax_nopriv_myaction', 'myaction_function');
    function myaction_function8() {
    
          echo pll_currentlanguage();
        die()
    }

    this code return error

    Call to undefined method Polylang_Admin_Filters::current_language()

    I can’t get my translated strings.
    Any have got a solution?

    Thanks!

    http://ww.wp.xz.cn/extend/plugins/polylang/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Chouby

    (@chouby)

    Both function are available *on frontend only* (not on the admin side). Since I never used ajax on frontend, I don’t know how it works. I just know how to use ‘wp_ajax_…’ actions on admin side. I will try to look further at this.

    Thread Starter g100g

    (@g100g)

    Thanks! Will be very useful!|

    Plugin Author Chouby

    (@chouby)

    The problem is that I try to make Polylang as light as possible and thus frontend and admin are not loaded together. Unfortunately, ajax requests set WP_ADMIN to true even on frontend 🙁 and since I test only this, the admin side of Polylang is loaded (instead of the frontend side you need).

    I will try to improve this, but I will need some time to be sure I do not break something.

    Waiting for this, you can hack the plugin in polylang.php around line 90 by replacing

    elseif (is_admin()) {

    with something like this:

    elseif (is_admin() && !(isset($_POST['action']) && $_POST['action'] == 'myaction')) {

    Thread Starter g100g

    (@g100g)

    Thank you. I will try this solutions.
    Hope to see this implementation in the next release.

    PS: Maybe you can use the WP constant DOING_AJAX to check the ajax actions, and a POST variable “polylang” to activate polylang only in some case.

    Plugin Author Chouby

    (@chouby)

    Great ! That’s probably the best to do. I propose that I modify the line 90 as follows:

    elseif (is_admin() && !(defined('DOING_AJAX') && isset($_REQUEST['pll_load_front']))) {

    And then you set ‘pll_load_front’ as hidden field (or GET variable) to load the frontend side of Polylang for your ajax request.

    Thread Starter g100g

    (@g100g)

    Hi,
    thanks for new version.
    I tried this version with AJAX request but i got problem to use pll_e, pll__ and pll_current_language functions.

    The problem is I have to specify in ajax request the language i want to use.
    I set language code in pll_load_front and I modified core.php to load right language according the value of pll_load_front variable.

    I modified this function

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    And this

    [Ditto]

    Hope this help to improve this wonderful plugin!
    Best.

    Plugin Author Chouby

    (@chouby)

    Sorry for late answer. I got the code in the e-mail I received. Obviously I shot too quick. Without testing… Thanks for reporting bug and proposed solution. Of course the admin_init filter is mandatory to choose the language. However I propose to modify the get_current_language function with these lines instead:

    elseif (isset($_REQUEST['pll_load_front']))
    	$lang =  isset($_REQUEST['lang']) && $_REQUEST['lang'] ? $this->get_language($_REQUEST['lang']) : $this->get_preferred_language();

    My idea is to get this:
    pll_load_front=1 -> allows you to load the current language thanks to the cookie
    lang=en&pll_load_front=1 -> allows you to force the language (you can even force the current language if you don’t want to rely on cookie).

    What do you think of this ? Does it correctly fit your needs ?

    Thread Starter g100g

    (@g100g)

    Your solution is fine for me. Using the cookie is always a right thing.

    What about the code to add to line 27

    if ((defined('DOING_AJAX') && isset($_REQUEST['pll_load_front']))) {
    			add_action('admin_init', array(&$this, 'load_textdomains'));
    		}

    Do you think is right?

    Thanks!

    Plugin Author Chouby

    (@chouby)

    Yes the add_action line is mandatory. The test itself may be useless thanks to the tests already done in Polylang::__construct (line 91 of polylang.php).

    You can consider the v0.8.8 will include these modifications. Thanks again for the suggestions.

    Thread Starter g100g

    (@g100g)

    Thank you for the support 🙂

    Thank you Chouby and g100g. I just came across this “need” today and it’s working “peaches”. Well done 🙂
    I love this plugin!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘[Plugin: Polylang] Ajax Request Problem’ is closed to new replies.