_wp_scripts_add_args_data()
Adds the data for the recognized args and warns for unrecognized args.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
null. Nothing (null).
Usage
_wp_scripts_add_args_data( $wp_scripts, $handle, $args ): void;
- $wp_scripts(WP_Scripts) (required)
- WP_Scripts instance.
- $handle(string) (required)
- Script handle.
- $args(array) (required)
- Array of extra args for the script.
Notes
- See: wp_enqueue_script()
- See: wp_register_script()
Changelog
| Since 7.0.0 | Introduced. |
_wp_scripts_add_args_data() wp scripts add args data code WP 7.0
function _wp_scripts_add_args_data( WP_Scripts $wp_scripts, string $handle, array $args ): void {
$allowed_keys = array( 'strategy', 'in_footer', 'fetchpriority', 'module_dependencies' );
$unknown_keys = array_diff( array_keys( $args ), $allowed_keys );
if ( ! empty( $unknown_keys ) ) {
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
$function_name = ( $trace[1]['class'] ?? '' ) . ( $trace[1]['type'] ?? '' ) . ( $trace[1]['function'] ?? __FUNCTION__ );
_doing_it_wrong(
$function_name,
sprintf(
/* translators: 1: $args, 2: List of unrecognized keys, 3: List of supported keys. */
__( 'Unrecognized key(s) in the %1$s param: %2$s. Supported keys: %3$s' ),
'$args',
implode( wp_get_list_item_separator(), $unknown_keys ),
implode( wp_get_list_item_separator(), $allowed_keys )
),
'7.0.0'
);
}
$in_footer = ! empty( $args['in_footer'] );
if ( $in_footer ) {
$wp_scripts->add_data( $handle, 'group', 1 );
}
if ( ! empty( $args['strategy'] ) ) {
$wp_scripts->add_data( $handle, 'strategy', $args['strategy'] );
}
if ( ! empty( $args['fetchpriority'] ) ) {
$wp_scripts->add_data( $handle, 'fetchpriority', $args['fetchpriority'] );
}
if ( ! empty( $args['module_dependencies'] ) ) {
$wp_scripts->add_data( $handle, 'module_dependencies', $args['module_dependencies'] );
/*
* A classic script with module dependencies must either be printed in the
* footer or use the 'defer' loading strategy. Otherwise, the script may be
* evaluated before the script modules import map is printed, causing
* dynamic imports to fail with a "Failed to resolve module specifier" error.
*/
$is_deferred = 'defer' === ( $args['strategy'] ?? null );
if ( ! $in_footer && ! $is_deferred ) {
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
$function_name = ( $trace[1]['class'] ?? '' ) . ( $trace[1]['type'] ?? '' ) . ( $trace[1]['function'] ?? __FUNCTION__ );
_doing_it_wrong(
$function_name,
sprintf(
/* translators: 1: 'module_dependencies', 2: Script handle, 3: 'in_footer', 4: 'strategy', 5: 'defer'. */
__( 'When the %1$s arg is provided, the "%2$s" script must either be printed in the footer (%3$s set to true) or use a deferred loading %4$s (%5$s) so that the import map is printed before the script is evaluated.' ),
'<code>module_dependencies</code>',
$handle,
'<code>in_footer</code>',
'<code>strategy</code>',
'<code>defer</code>'
),
'7.0.0'
);
}
}
}