automator_asset_script_dependencies_{dynamic}
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the final list of script dependencies for a registered asset. Allows adding context-specific dependencies. Filters script dependencies for registered assets, allowing context-specific additions before scripts are enqueued.
add_filter( 'automator_asset_script_dependencies_{dynamic}', $callback, 10, 3 );
Description
Fires after script dependencies are calculated for a registered asset. Developers can use this dynamic hook to conditionally add or remove script dependencies based on the asset name and enqueue options. Modify `$final_script_deps` to alter the script's dependencies.
Usage
add_filter( 'automator_asset_script_dependencies_{dynamic}', 'your_function_name', 10, 3 );
Parameters
-
$final_script_deps(array) - The calculated script dependencies.
-
$asset_name(string) - The base name of the asset.
-
$options(array) - The options passed to enqueue_asset.
Return Value
The filtered value.
Examples
/**
* Add a custom dependency to a specific Automator script if it meets certain criteria.
*
* This example shows how you might add a dependency to a script named 'my-custom-script'
* only if the 'is_admin_area' option is true and the asset being enqueued is
* associated with the 'user_automation' module.
*
* @param array $final_script_deps The calculated script dependencies.
* @param string $asset_name The base name of the asset.
* @param array $options The options passed to enqueue_asset.
* @return array The modified script dependencies.
*/
add_filter( 'automator_asset_script_dependencies_my-custom-script', function( $final_script_deps, $asset_name, $options ) {
// Check if the script is intended for the admin area and if it's related to user automations.
if ( isset( $options['is_admin_area'] ) && true === $options['is_admin_area'] && isset( $options['module'] ) && 'user_automation' === $options['module'] ) {
// Add a custom dependency if it's not already present.
if ( ! in_array( 'another-script-dependency', $final_script_deps, true ) ) {
$final_script_deps[] = 'another-script-dependency';
}
}
return $final_script_deps;
}, 10, 3 );
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:1068
*
* @since 6.7
*
* @param array $final_script_deps The calculated script dependencies.
* @param string $asset_name The base name of the asset.
* @param array $options The options passed to enqueue_asset.
*/
$final_script_deps = apply_filters( 'automator_asset_script_dependencies_' . $handle, $final_script_deps, $asset_name, $options );
// Flags to track successful registration.
$registered_script = false;
$registered_style = false;
// Register the style if its corresponding CSS file exists.
if ( file_exists( $css_file_path ) ) {