admin管理员组文章数量:1278979
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);
}
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);
}
Share
Improve this question
edited Oct 17, 2021 at 10:03
user2059370
asked Oct 17, 2021 at 7:07
user2059370user2059370
16511 bronze badges
5
|
1 Answer
Reset to default 1The 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:
- A function,
my_icl_set_current_language()
. - The code inside the function that changes the language.
- 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');
本文标签: Use value from Ajax call in PHP function
版权声明:本文标题:Use value from Ajax call in PHP function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266059a2368484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
global $sitepress;
. If that doesn't work then the issue is probably specific to WPML. – Jacob Peattie Commented Oct 17, 2021 at 9:44echo 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. – Jacob Peattie Commented Oct 17, 2021 at 9:59