How to Object.freeze wp_localize_script
No, it is not possible to do this from wp_localize_script(). You would need to add add Object.freeze( text_ajax ); to the beginning of your script.
No, it is not possible to do this from wp_localize_script(). You would need to add add Object.freeze( text_ajax ); to the beginning of your script.
You should call wp_localize_script() in the wp_enqueue_scripts hook (or a later hook) in which you would get the correct product/post data such as ID, and that conditional tags like is_single() would also work as expected. So you should move all the $product, $product_id, $dataToBePassed and wp_localize_script() parts to inside the wp_enqueue_scripts callback, and then you’d … Read more
Get localize of a loaded javascript
Why the wp_localize_script() does not work correct in my case? ( /wp-admin/admin-ajax.php 400 (Bad Request) )
I do it this way and it works fine. Make sure you don’t have jQuery defined anywhere else on your site (like manually in the header), as some browsers don’t like that. /** * Load assets on initiation */ add_action(‘init’, ‘load_assets’); function load_assets(){ wp_enqueue_scripts(‘jquery’); }
First issue- you can’t access any data because your AJAX request is an entirely separate request from the one that loaded the page you’re making the request from. This is not unique to WordPress. You have to pass the data you want to operate on along with your AJAX request. Second issue- calling your plugin … Read more
1st) Add another dependency: array( ‘json2’, ‘jquery’ );. 2nd) Dig into wp-localize-script and read about wp-localize-script() in Codex. It allows you to send data from PHP to javascript via an javascript var, set in PHP (as 2nd arg for wp_localize_script()).
It doesn’t work because you haven’t enqueued your script properly. If the script isn’t printed, neither are the variables set by wp_localize_script. Please read a WP Codex entry on wp_localize_script function. You have to include a path to the script after the handle.
I don’t know if there is really a right answer to this question, and I don’t know what you find cumbersome about wp_localize_script, but you should be able to do it either way. The difference is that with wp_localize_script the data is printed to the source of the page, which you wouldn’t want if the … Read more
Maybe I am missing the point but assuming that a script is enqueued with the lu_ban slug… function add_data_admin_wpse_112178() { $data = get_option(‘lu_ban_data’); wp_localize_script( ‘lu_ban’, ‘lu_ban_object’, $data); } add_action(‘admin_enqueue_scripts’,’add_data_admin_wpse_112178′);