Title: [Plugin: Polylang] Ajax Request Problem
Last modified: August 20, 2016

---

# [Plugin: Polylang] Ajax Request Problem

 *  [g100g](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/)
 * 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://wordpress.org/extend/plugins/polylang/](http://wordpress.org/extend/plugins/polylang/)

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

 *  Plugin Author [Chouby](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781856)
 * 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](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781857)
 * Thanks! Will be very useful!|
 *  Plugin Author [Chouby](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781862)
 * 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](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781863)
 * 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](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [14 years ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781885)
 * 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](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781899)
 * 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](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   The maximum number of lines of code that you can post in these forums is **ten
   lines**. Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  And this
 * _[Ditto]_
 * Hope this help to improve this wonderful plugin!
    Best.
 *  Plugin Author [Chouby](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781919)
 * 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](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781920)
 * 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](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781921)
 * 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](https://wordpress.org/support/users/g100g/)
 * (@g100g)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2781922)
 * Thank you for the support 🙂
 *  [Gonçalo Peres](https://wordpress.org/support/users/gonperesgmailcom/)
 * (@gonperesgmailcom)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2782005)
 * 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.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)

 * 11 replies
 * 3 participants
 * Last reply from: [Gonçalo Peres](https://wordpress.org/support/users/gonperesgmailcom/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-polylang-ajax-request-problem/#post-2782005)
 * Status: not resolved