Filter
Since 6.7
Dynamic
uncanny-automator
automator_asset_script_data_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the data before it's encoded and added inline. Filters the script data before it's encoded and added inline to dynamic contexts.
add_filter( 'automator_asset_script_data_{dynamic}', $callback, 10, 1 );
Description
Fires before script data is JSON encoded and enqueued. Use this dynamic hook to modify or add to the data passed to your JavaScript. The `{dynamic}` portion of the hook name is the script handle.
Usage
add_filter( 'automator_asset_script_data_{dynamic}', 'your_function_name', 10, 1 );
Parameters
-
$data(array) - The data to be filtered.
Return Value
array The filtered data.
Examples
add_filter( 'automator_asset_script_data_my_custom_script', 'my_plugin_modify_automator_script_data', 10, 1 );
/**
* Modifies the data passed to a specific Automator script handle.
*
* This example demonstrates how to add or modify a specific piece of data
* that will be available in your JavaScript file under the 'my_custom_data' key.
*
* @since 1.0.0
*
* @param array $data The original data array passed by the Automator script.
* @return array The modified data array.
*/
function my_plugin_modify_automator_script_data( $data ) {
// Check if 'some_setting' exists and if it's enabled.
// This is a hypothetical check based on a plugin setting.
if ( ! empty( $data['some_setting'] ) && true === $data['some_setting'] ) {
// Add a new key-value pair to the data array.
// This data will be accessible in JavaScript like: automator_script_data.my_custom_data
$data['my_custom_data'] = 'This data is only added when some_setting is true!';
} else {
// Optionally, you could add default data or skip adding data altogether.
$data['my_custom_data'] = 'Default data when some_setting is false or not present.';
}
// Ensure we always return the modified array.
return $data;
}
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:1136
* Filters the data before it's encoded and added inline.
*
* @since 6.7
*
* @param array $data The data to be filtered.
* @return array The filtered data.
*/
$filtered_data = apply_filters( 'automator_asset_script_data_' . $handle, $data );
// Safely encode the PHP data into a JSON string suitable for JavaScript.
$encoded_data = wp_json_encode( $filtered_data );
// Check for JSON encoding errors, especially important if data structure is complex or from external sources.
if ( false === $encoded_data ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {