0

I have a ajax script that will take a value from a select from on onchange. This works well, and with console.log I can see the correct value. My select form triggers a reload, and I see the value in console before the page reloads.

I want to use this value to set a backend language for WPML, but unsure how to use the value.

PHP

function my_action( ) {
 $dropdown_shop_order_language = $_POST['dropdown_shop_order_language'];
      
 echo json_encode($dropdown_shop_order_language);
 wp_die();

}
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');

This will give me a value of de, en or nl

Example of function to change language:

add_action('wp_loaded', 'my_icl_set_current_language'); 
function my_icl_set_current_language() {
    global $sitepress;
    $sitepress->switch_lang('de');
}

something like this is what Im after:

 $sitepress->switch_lang($dropdown_shop_order_language);

Whats the best approach here?

This doesnt work:

add_action('wp_loaded', 'my_icl_set_current_language'); 
function my_icl_set_current_language() {
    global $sitepress;
    $sitepress->switch_lang($dropdown_shop_order_language);
}
5
  • What's wrong with the "something like this code"? If that doesn't work then make sure you have global $sitepress;. If that doesn't work then the issue is probably specific to WPML. Oct 17, 2021 at 9:44
  • HI @JacobPeattie when I set the language manually, like in my first example it works. When I add the variable from my Ajax call nothing happens. So Seems the value isn't sent to my second function. Or should it be passed dong it this way? Oct 17, 2021 at 9:53
  • If echo json_encode($dropdown_shop_order_language); is giving the correct value, then you're passing the value correctly. How are you trying to use it? Please include the full code you're trying. Oct 17, 2021 at 9:59
  • Yes, I do get the right value in console. I updated with the function im trying, not sure if Im firing the hook at the wrong place, or if the value is passed correctly. The onchange i getting the value from is triggering a page load so I need my language function to fire before the page is reloaded. I added the script im trying to my question. Oct 17, 2021 at 10:05
  • If I echo the variable of the WPML function it displays in the console after the pageload, where the Ajax value displays during the pageload. If I try to add the variable from the Ajax request to the WPML function its empty after the page reloads. Oct 17, 2021 at 14:29

1 Answer 1

1

The main problem with what you're attempting is that variables do not persist across requests. If you set a variable on the AJAX request, that variable will not be set on the request to reload the page.

The other problem is the way you're referring to this as an "Example of function to change language":

add_action('wp_loaded', 'my_icl_set_current_language'); 
function my_icl_set_current_language() {
    global $sitepress;
    $sitepress->switch_lang($dropdown_shop_order_language);
}

There's 3 things here:

  1. A function, my_icl_set_current_language().
  2. The code inside the function that changes the language.
  3. A call to add_action() which tells WordPress when to run the function.

You only need #2, and you just need to put it inside your AJAX callback:

function my_action( ) {
    global $sitepress;
    $dropdown_shop_order_language = $_POST['dropdown_shop_order_language'];

    $sitepress->switch_lang($dropdown_shop_order_language);
}
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.