admin管理员组文章数量:1277315
I'm creating a plugin and I want to get the list of all scripts and CSS used by other plugins.
This is my function:
function crunchify_print_scripts_styles() {
$result = [];
$result['scripts'] = [];
$result['styles'] = [];
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
$result['scripts'][] = $wp_scripts->registered[$script]->src . ";";
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
$result['styles'][] = $wp_styles->registered[$style]->src . ";";
endforeach;
return $result;
}
add_action( 'wp_enqueue_scripts', 'crunchify_print_scripts_styles');
I want to get the returned value inside a variable.
I tried this:
$toto = do_action( 'crunchify_print_scripts_styles' );
var_dump( $toto );
And this is my result:
NULL
If I write echo
inside every foreach
loop, I get the correct results, but how to store these values inside a variable?
[edit]
My code inside a pluginm which is not working too
/**
* Get all scripts and styles from Wordpress
*/
function print_scripts_styles() {
$result = [];
$result['scripts'] = [];
$result['styles'] = [];
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
$result['scripts'][] = $wp_scripts->registered[$script]->src . ";";
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
$result['styles'][] = $wp_styles->registered[$style]->src . ";";
endforeach;
return $result;
}
add_action( 'wp_head', 'wp_rest_assets_init');
/**
* Init JSON REST API Assets routes.
*
* @since 1.0.0
*/
function wp_rest_assets_init() {
$all_the_scripts_and_styles = print_scripts_styles();
if ( ! defined( 'JSON_API_VERSION' ) &&
! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins' ) ) ) {
$class = new WP_REST_Assets();
$class::$scriptsAndStyles = $all_the_scripts_and_styles;
add_filter( 'rest_api_init', array( $class, 'register_routes' ) );
} else {
$class = new WP_JSON_Menus();
add_filter( 'json_endpoints', array( $class, 'register_routes' ) );
}
}
add_action( 'init', 'wp_rest_assets_init' );
I'm creating a plugin and I want to get the list of all scripts and CSS used by other plugins.
This is my function:
function crunchify_print_scripts_styles() {
$result = [];
$result['scripts'] = [];
$result['styles'] = [];
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
$result['scripts'][] = $wp_scripts->registered[$script]->src . ";";
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
$result['styles'][] = $wp_styles->registered[$style]->src . ";";
endforeach;
return $result;
}
add_action( 'wp_enqueue_scripts', 'crunchify_print_scripts_styles');
I want to get the returned value inside a variable.
I tried this:
$toto = do_action( 'crunchify_print_scripts_styles' );
var_dump( $toto );
And this is my result:
NULL
If I write echo
inside every foreach
loop, I get the correct results, but how to store these values inside a variable?
[edit]
My code inside a pluginm which is not working too
/**
* Get all scripts and styles from Wordpress
*/
function print_scripts_styles() {
$result = [];
$result['scripts'] = [];
$result['styles'] = [];
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
$result['scripts'][] = $wp_scripts->registered[$script]->src . ";";
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
$result['styles'][] = $wp_styles->registered[$style]->src . ";";
endforeach;
return $result;
}
add_action( 'wp_head', 'wp_rest_assets_init');
/**
* Init JSON REST API Assets routes.
*
* @since 1.0.0
*/
function wp_rest_assets_init() {
$all_the_scripts_and_styles = print_scripts_styles();
if ( ! defined( 'JSON_API_VERSION' ) &&
! in_array( 'json-rest-api/plugin.php', get_option( 'active_plugins' ) ) ) {
$class = new WP_REST_Assets();
$class::$scriptsAndStyles = $all_the_scripts_and_styles;
add_filter( 'rest_api_init', array( $class, 'register_routes' ) );
} else {
$class = new WP_JSON_Menus();
add_filter( 'json_endpoints', array( $class, 'register_routes' ) );
}
}
add_action( 'init', 'wp_rest_assets_init' );
Share
Improve this question
edited Jul 27, 2016 at 9:57
Edouard Kombo
asked Jul 25, 2016 at 14:44
Edouard KomboEdouard Kombo
3331 gold badge2 silver badges7 bronze badges
7
|
Show 2 more comments
5 Answers
Reset to default 22do_action
doesn't quite work like that. When you call do_action('crunchify_print_scripts_styles')
WP looks at its list of registered actions and filters for any that are attached to a hook called crunchify_print_scripts_styles
and then runs those functions.
And you probably want to remove this:
add_action( 'wp_enqueue_scripts', 'crunchify_print_scripts_styles');
... because you aren't able to get the return result of your function.
Also when you use this particular hook you can't guarantee that other functions don't enqueue more scripts or styles after you've generated your list. Use a hook that fires after all scripts and styles have been enqueued, such as wp_head, for convenience, or better still just call your function within your theme when you want to display the result.
Reworking your code like this should work...
function crunchify_print_scripts_styles() {
$result = [];
$result['scripts'] = [];
$result['styles'] = [];
// Print all loaded Scripts
global $wp_scripts;
foreach( $wp_scripts->queue as $script ) :
$result['scripts'][] = $wp_scripts->registered[$script]->src . ";";
endforeach;
// Print all loaded Styles (CSS)
global $wp_styles;
foreach( $wp_styles->queue as $style ) :
$result['styles'][] = $wp_styles->registered[$style]->src . ";";
endforeach;
return $result;
}
Then within your theme:
print_r( crunchify_print_scripts_styles() );
... will show you the results for debugging, or of course...
$all_the_scripts_and_styles = crunchify_print_scripts_styles();
... will give you the list to manipulate.
Calling it in the theme makes sure you call it after all scripts and styles are enqueued.
To call it from your plugin, attach it to any hook that runs later than wp_enqueue_scripts, like wp_head as I mentioned above:
add_action( 'wp_head', 'wpse_233142_process_list');
function wpse_233142_process_list() {
$all_the_scripts_and_styles = crunchify_print_scripts_styles();
// process your array here
}
You could use wp_print_scripts
and wp_print_styles
actions to timely and properly access to enqueued scripts and styles, as these actions are the last events before scripts and styles are included in the document and, because of that, the last event where modifications on $wp_styles
or $wp_scripts
could have effect on styles and scripts included in the document.
So, they are the events where you can be more confident that $wp_styles
and $wp_scripts
contain the scripts and styles effectively included in the document.
add_action( 'wp_print_scripts', 'cyb_list_scripts' );
function cyb_list_scripts() {
global $wp_scripts;
$enqueued_scripts = array();
foreach( $wp_scripts->queue as $handle ) {
$enqueued_scripts[] = $wp_scripts->registered[$handle]->src;
}
}
add_action( 'wp_print_styles', 'cyb_list_styles' );
function cyb_list_styles() {
global $wp_styles;
$enqueued_styles = array();
foreach( $wp_styles->queue as $handle ) {
$enqueued_styles[] = $wp_styles->registered[$handle]->src;
}
}
If you declare $enqueued_scripts
adn $enqueued_styles
as global variables (or any other valid scope, for example you could store it in a method's property), you could access to the list of script and styles in a later action.
For example (just a quick example):
global $enqueued_scripts;
global $enqueued_styles;
add_action( 'wp_print_scripts', 'cyb_list_scripts' );
function cyb_list_scripts() {
global $wp_scripts;
global $enqueued_scripts;
$enqueued_scripts = array();
foreach( $wp_scripts->queue as $handle ) {
$enqueued_scripts[] = $wp_scripts->registered[$handle]->src;
}
}
add_action( 'wp_print_styles', 'cyb_list_styles' );
function cyb_list_styles() {
global $wp_styles;
global $enqueued_styles;
$enqueued_styles = array();
foreach( $wp_styles->queue as $handle ) {
$enqueued_styles[] = $wp_styles->registered[$handle]->src;
}
}
add_action( 'wp_head', function() {
global $enqueued_scripts;
var_dump( $enqueued_scripts );
global $enqueued_styles;
var_dump( $enqueued_styles );
} );
my solution for this is :
- make an option field in db
- activate sessions and secure them
Functions I used
I've start whit two functions from user cybmeta : just add a handle to result.
function theme_list_scripts() {
global $wp_scripts;
global $enqueued_scripts;
$enqueued_scripts = array();
foreach( $wp_scripts->queue as $handle ) {
$enqueued_scripts[] = $handle." | ".$wp_scripts->registered[$handle]->src;
}
return $enqueued_scripts;
}
function theme_list_styles() {
global $wp_styles;
global $enqueued_styles;
$enqueued_styles = array();
foreach( $wp_styles->queue as $handle ) {
$enqueued_styles[] = $handle." | ".$wp_styles->registered[$handle]->src;
}
return $enqueued_styles;
}
add_action( 'wp_print_scripts', 'wpcustom_inspect_scripts_and_styles' );
function wpcustom_inspect_scripts_and_styles(){
$loadet_list = array();
$loadet_list["style"] = implode( " ; ",theme_list_styles() );
$loadet_list["script"] = implode( " ; ",theme_list_scripts() );
$_SESSION["front-end-list"] = implode("{}",$loadet_list);
}
function front_loadet_files_list(){
update_option( 'front_incl_list', $_SESSION["front-end-list"] );
}
in header file call after wp_head() befor colosing head tag :
front_loadet_files_list();
in admin side in the file template where you wanna display :
get the data from option field in db
$loadet_list = prep_front_list("front-end-list");
call it for displaying
display_front_list($loadet_list["styles"]);
display_front_list($loadet_list["scripts"]);
the result
If you truly want to get a list of all styles, you can use the new 'script_loader_tag' filter (Since Version 4.1).
The "wp_print_scripts" is:
Called by admin-header.php and ‘wp_head’ hook.
i.e it doesn't show scripts in the footer.
References:
Add Defer & Async Attributes to WordPress Scripts
wp_print_scripts
to get registered scripts you need function wp_scripts() and to get all enqueued styles you can use the function wp_styles(). Adding proper priority is also an important aspect to make sure to list the scripts after all of them were registered
something like this:
<?php
/*
Plugin name: My Plugin to list scripts and styles
*/
add_action( 'wp_enqueue_scripts', 'my_plugin_print_styles', PHP_INT_MAX );
function my_plugin_print_styles(){
$wp_scripts = wp_scripts();
$wp_styles = wp_styles();
var_dump($wp_scripts);
var_dump($wp_styles);
}
本文标签: How can I get a list of all enqueued scripts and styles
版权声明:本文标题:How can I get a list of all enqueued scripts and styles? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292173a2370615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
do_action
does not return a result, and besides, the action has already taken place atwp_enqueue_scripts
... easier just to create a global, eg.global $crunchifyenqueued; $crunchifyenqueued = $result;
then again call the global in your later function to access the variable. – majick Commented Jul 25, 2016 at 14:54apply_filters
then? you can easily get the return value from that. – majick Commented Jul 25, 2016 at 15:57