issue in wp_localize_script
Change the first param of wp_localize_script to same as the handle of your main script file. main in this case.
Change the first param of wp_localize_script to same as the handle of your main script file. main in this case.
Instead of passing count through wp_localize_script you should just handle the JS logic in JS. For example in JS: var count = document.querySelectorAll( ‘input[name^=”link”]’ ).length; That would get the count of all input elements where the name attribute starts with link. It would be more performant to scope the selector by adding a class to … Read more
that’s because you have defined the variable theUser as null and overridden the one that has the object before then you have tried to call the object. just remove this line var theUser;
I just had this issue myself and was able to fix it by assigning it to the script that required the data and not to “jquery”, example below: wp_localize_script(‘jquery’, ‘vars’, $vars); wp_localize_script( ‘replace-with-your-script-name’, ‘vars’, $vars); To clarify: This would be the same name that was used to identify in wp_enqueue_script() and should match it.
wp_localize_script() defines a variable in the global JS scope – in the case of your code, the object is placed in a globally-scoped variable with the identifier MediaFolders. In your load event callback you are also defining a variable with the identifier MediaFolders scoped to it’s encapsulating function body – so when you use the … Read more
If lu_ban_object.method equals the string wrap, and you’d like to use that string to call jQuery’s wrap() method, you’d use bracket notation : jQuery(function($) { $(‘.advert’)[lu_ban_object.method](‘<div>Hello World</div>’); });
Accessing the data var data0A = theme_metadata[0].dataA, data0B = theme_metadata[0].dataB, data1A = theme_metadata[1].dataA, data1B = theme_metadata[1].dataB; [Update] Iterating over numeric arrays in JS While the initial question was still somewhat pertinent to WordPress, your follow-up really is a question purely concerning javascript syntax. Anyway, this is how: var dataA, dataB; for ( var i = … Read more
If you want to use the variables from the admin.js script, and you want to be sure it works you have to use as handle for wp_localize_script the handle of the script that have to use data call wp_localize_script after that script is enqueued wp_enqueue_script( ‘some_handle’ ); wp_enqueue_script(“admin”, $adminDir.”js/admin.js”, false, “1.0”); $translation_array = array( ‘some_string’ … Read more
get_user_meta returns an array, so to get an array containing an array of each user’s data, use [] to append: foreach( $users as $user ){ $veik[] = get_user_meta( $user->ID ); }
You don’t need to use json_encode() when using wp_localize_script(). The last argument of wp_localize_script() should be a PHP array. This will be converted to JSON for you. So to resolve the issue, remove this line: $location_json = json_encode($location_array); And change wp_localize_script() to use $location_array: wp_localize_script(‘sage/main.js’, ‘locationJSON’, $location_array); Because $location_array had been double-encoded the locationJSON variable … Read more