automator_asset_script_localize_vars_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the entire array of variables to be localized for a specific script handle. Allows adding, removing, or modifying top-level JavaScript variables and their data before they are added inline. Filters all localized script variables for a specific asset handle before it's enqueued.
add_filter( 'automator_asset_script_localize_vars_{dynamic}', $callback, 10, 4 );
Description
Filter the JavaScript variables localized for a specific script. Use this hook to dynamically add, remove, or modify top-level variables before they're enqueued. It's triggered after the default variables are prepared.
Usage
add_filter( 'automator_asset_script_localize_vars_{dynamic}', 'your_function_name', 10, 4 );
Parameters
-
$localizable_vars(array) - The array of variables to localize [ 'JsObjectName' => $data_array, ... ].
-
$handle(string) - The script handle.
-
$asset_name(string) - The base name of the asset.
-
$options(array) - The original options passed to enqueue_asset.
Return Value
The filtered value.
Examples
/**
* Example of how to use the automator_asset_script_localize_vars_{dynamic} filter hook.
* This example demonstrates how to conditionally modify localized variables
* for a specific script handle ('my-custom-script') based on whether
* the user is an administrator.
*/
add_filter( 'automator_asset_script_localize_vars_my-custom-script', function( $localizable_vars, $handle, $asset_name, $options ) {
// Only modify variables if the current user is an administrator
if ( current_user_can( 'manage_options' ) ) {
// Add a new key-value pair to the existing localized variables.
// This assumes the JavaScript will look for a variable named 'myAdminData'.
if ( ! isset( $localizable_vars['myAdminData'] ) ) {
$localizable_vars['myAdminData'] = [];
}
$localizable_vars['myAdminData']['isAdmin'] = true;
$localizable_vars['myAdminData']['adminMessage'] = __( 'Welcome, Administrator!', 'your-text-domain' );
// You could also modify existing variables if needed.
if ( isset( $localizable_vars['generalData'] ) && is_array( $localizable_vars['generalData'] ) ) {
$localizable_vars['generalData']['userRole'] = 'administrator';
}
} else {
// For non-administrators, you might set different default values or skip adding admin-specific data.
if ( ! isset( $localizable_vars['myAdminData'] ) ) {
$localizable_vars['myAdminData'] = [];
}
$localizable_vars['myAdminData']['isAdmin'] = false;
$localizable_vars['myAdminData']['adminMessage'] = __( 'You are not an administrator.', 'your-text-domain' );
}
// Always return the modified (or unmodified) array of localizable variables.
return $localizable_vars;
}, 10, 4 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/class-utilities.php:1118
* @since 6.7
*
* @param array $localizable_vars The array of variables to localize [ 'JsObjectName' => $data_array, ... ].
* @param string $handle The script handle.
* @param string $asset_name The base name of the asset.
* @param array $options The original options passed to enqueue_asset.
*/
$localizable_vars = apply_filters( 'automator_asset_script_localize_vars_' . $handle, $localizable_vars, $handle, $asset_name, $options );
// Add inline data using wp_add_inline_script if script was registered and data exists.
if ( $registered_script && ! empty( $localizable_vars ) && is_array( $localizable_vars ) ) {
// Iterate over the potentially filtered array of variables
foreach ( $localizable_vars as $object_name => $data ) {
// Basic validation for the JavaScript variable name and data structure.
// Note: User must ensure $object_name is a strictly valid JS variable identifier.