How to prevent wp_localize_script from localize the ajax URL in the array?
-
Hi,
Currently in my WordPress plugin, I use wp_localize_script to pass data from PHP to JS, as below:
wp_localize_script($this->plugin_name, 'MyObj', array( 'ajax_url' => admin_url('admin-ajax.php'), ));
It works properly in English post with a shortcode of the plugin. However, in the Chinese post with the same shortcode, it does not. After tracing the data, I find in the Chinese post, the ajax_url will be added a language slug.
For example, if the English post is https://www.example.com/blogs/jpg-to-png/, then the ajax_url will be https://www.example.com/blogs/wp-admin/admin-ajax.php, which is correct.
But in the Chinese version, https://www.example.com/zh-CN/blogs/jpg-to-png/, then ajax_url will also be changed to https://www.example.com/zh-CN/blogs/wp-admin/admin-ajax.php, which is not desired.
How to prevent this? I try to pass the root_url, admin_url, but wp_localize_script will always add the language slug for the Chinese post. How to keep the data intact when passing it from PHP to JS?
- You must be logged in to reply to this topic.