php - 在哪里使用 switch_to_locale() WordPress 功能?
问题描述
我已经制作了一个表格以使用switch_to_locale()
功能切换语言
表格提交给admin_url('admin-post.php')
然后我处理它:
add_action('admin_post_change_lang_action', 'switch_site_language');
add_action('admin_post_nopriv_change_lang_action', 'switch_site_language');
function switch_site_language(){
if(isset($_POST['_change_lang_nonce']) && wp_verify_nonce($_POST['_change_lang_nonce'],
'change_lang_action', false) && isset($_POST['lang'])) {
switch_to_locale($_POST['lang']);
}
wp_safe_redirect(wp_get_referer());
}
但是当我只运行 switch_to_locale('ru_RU'); 时它不起作用 从外面它可以工作
这里有什么问题吗
提前致谢。
解决方案
我实际上找不到原因,但我找到了解决这个问题的好方法
我像这样提交了表格
<form action="" method="GET">...</form>
对于处理程序:
add_action( 'wp_loaded', function () {
if(isset($_GET['_change_lang_nonce']) && isset($_GET['lang'])){
if(wp_verify_nonce($_GET['_change_lang_nonce'], 'change_lang_action', false)) {
if(switch_to_locale( $_GET['lang'] )){
wc_add_notice(__('Language changed with success'), 'success' );
}else{
wc_add_notice(__( 'Some thing Went Wrong please try later!'), 'error' );
}
}else{
wc_add_notice(__( 'Some thing Went Wrong please try later!'), 'error' );
}
wp_safe_redirect(wp_get_referer());
}
});
推荐阅读
- java - openapi-generator-maven-plugin 为枚举添加引号,swagger-ui 使用它们导致错误
- jquery - 如何在 Rails 6 项目中包含 jquery-ui?
- java - 如何使 Sublime Text 3 在 Ubuntu 上通过终端编译和运行 java
- php - php file_put_contents 偶尔没有这样的文件或目录
- python - Hangman 无法一张一张更新图片
- android - 用于动态功能模块的合并测试 apk
- r - 在 R 中处理大整数
- json - 使用嵌套字段解析 json (Kotlin)
- python - 表 * 没有名为 user_id,django 的列
- javascript - 如何在画布中用 beginPath 写字母?