![]() The last chance to localize your script would then be on the 'wp_print_footer_scripts' hook. In this case, consider enqueueing your script with the in_footer argument set to true, to delay the printing of your script include until much later in the page build (ie: wp_enqueue_script( $slug, $URL, $deps, $ver, true ) ). This has some significant repercussions if you enqueue your script as you should using the appropriate actions ( wp_enqueue_scripts and admin_enqueue_scripts), but wish to localize later using data that is not available at enqueue time. IMPORTANT! wp_localize_script() MUST be called after the script has been registered using wp_register_script() or wp_enqueue_script().įurthermore, the actual output of the JavaScript a tag containing your localization variable occurs at the time that the enqueued script is printed (output/included on the page). ![]() Like json_encode(), the data will be a JavaScript object if the array is an associate array (a map), otherwise the array will be a JavaScript array. The data can be either a single- or multi- (as of 3.3) dimensional array. However, as this is a JavaScript object name, it cannot contain dashes. ![]() Thus, the value here should be properly prefixed with the slug or another unique value, to prevent conflicts. Note that this should be unique to both the script and to the plugin or theme. $object_name is the name of the variable which will contain the data. ` wp_localize_script() ` should only be used when you actually want to localize strings. wp_add_inline_script() was introduced in WordPress Version 4.5, and is now the best practice for that use case. Though localization is the primary use, it was often used to pass generic data from PHP to JavaScript, because it was originally the only official way to do that. This is necessary because WordPress currently only offers a localization API in PHP, not directly in JavaScript (but see ticket #20491). This lets you offer properly localized translations of any strings used in your script. This function localizes a registered script with data for a JavaScript variable. Return bool True if the script was successfully localized, false otherwise. I need this script to be called after those scripts. When I use the adminfooter action, the code is added before the JS tags that are called by the wpenqueuescript function. The data can be either a single or multi-dimensional array. I want to load an inline JavaScript tag in the footer of the admin.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |