Issue with wp_localize_script
-
Hi,
unfortunately I have not found a solution in https://wordpress.stackexchange.com and via google. I get the following error message when calling:Uncaught ReferenceError: ait_php_var is not defined ../assets/js/ait_buttons.js?wp-mce-4940-20190515 line 11
This is the corresponding function I use to pass the values to the JS function:
/* localization for ait_buttons.js */ add_action( 'wp_enqueue_scripts', 'ait_load_scripts' ); function ait_load_scripts() { $ait_pfad = plugin_dir_url( __FILE__ ) . '/assets/js/ait_buttons.js'; $ait_add_options = 'here'; // Enqueued script with localized data. wp_register_script('ait_js_script', $ait_pfad ); // Localize the script with new data wp_localize_script( 'ait_js_script', 'ait_php_var', array( 'external_link' => __( 'Ext. Link', 'add-infos-to-the-events-calendar' ) 'event_category' => __( 'Event Category', 'add-infos-to-the-events-calendar' ), 'internal_link' => __( 'Int. Link', 'add-infos-to-the-events-calendar' ), 'ackids' => $ait_add_options, ) ); // Enqueue the script after localizing wp_enqueue_script( 'ait_js_script', $ait_pfad ); }
Here is the relevant (sample) snippet from the JS file:
// JavaScript Document // alert( ait_php_var.ackids ); (function() { // const { __, } = wp.i18n; // var ait_cats = JSON.parse(ait_cat_obj.category); // var categories = []; var ait_http = 'https://'; var ait_https = 'https://'; var ait_test_http = ''; console.log(ait_php_var.ackids); ...
The file is in the correct folder, otherwise the error would not appear in line 11. The same error message also occurs when the other variable (e. g. external_link) is called in the same or similar form.
Thanks a lot for any help to solve the problem.
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Issue with wp_localize_script’ is closed to new replies.