The block of code beneath is meant to generate an inventory of dependencies for a given deal with
e.g. jquery
.
perform get_deps() {
international $wp_scripts;
$wp_scripts->all_deps( 'jquery' );
$output = []; // Initialize an empty array
foreach( $wp_scripts->to_do as $deal with ) {
$dep = wp_scripts()->registered[ $handle ];
$output [] = $dep->src;
}
$end result = $output;
return $end result;
}
The perform
beneath is a fast take a look at of the get_deps
perform to examine if it spits out the anticipated listing of dependencies.
perform display_scripts() {
print_r( get_deps() ) ;
}
add_action( 'wp_enqueue_scripts', 'display_scripts', PHP_INT_MAX );
This works as anticipated nevertheless if print_r( get_deps() ) ;
is named with the script_loader_tag
filter, it produces a sudden listing.
As a fast take a look at, if I change jquery
with lazysizes
which has no dependencies like so:
perform get_deps() {
international $wp_scripts;
$wp_scripts->all_deps( 'lazysizes' );
$output = []; // Initialize an empty array
foreach( $wp_scripts->to_do as $deal with ) {
$dep = wp_scripts()->registered[ $handle ];
$output [] = $dep->src;
}
$end result = $output;
return $end result;
}
And shortly validate it with:
perform display_scripts() {
print_r( get_deps() ) ;
}
add_action( 'wp_enqueue_scripts', 'display_scripts', PHP_INT_MAX );
The output is: Array ( )
. Up to now so good. Now if print_r( get_deps() ) ;
included in a block of code with the script_loader_tag
filter, the outcomes are fully totally different. Here is the take a look at:
perform take a look at() {
print_r( get_deps() ) ;
}
add_filter( 'script_loader_tag', 'take a look at', 10 );
which produces:
Array
(
[0] => /wp-includes/js/jquery/jquery.min.js
[1] => /wp-includes/js/jquery/jquery-migrate.min.js
[2] =>
[3] => https://area.com/wp-content/themes/theme-name/js/plugins.js
[4] => https://area.com/wp-content/themes/theme-name/js/script.js
)
Array
(
[0] => /wp-includes/js/jquery/jquery-migrate.min.js
[1] =>
[2] => https://area.com/wp-content/themes/theme-name/js/plugins.js
[3] => https://area.com/wp-content/themes/theme-name/js/script.js
)
I’ve no thought why it does this because it ought to be Array()
. What am I doing fallacious or overlooking?