6

我想知道如何通过wp_enqueue_script获取在 wordpress 中排队的脚本列表。我做了一些研究并检查了 wp 核心本身,但我能得到的最接近的是:

add_options_page('Wp Sysinfo', 'Wp Sysinfo', 'manage_options', 'wp-sysinfo', 'sysinfo_page');

function sysinfo_page(){
    global $wp_scripts;
    print_r($wp_scripts->queue);
}

但是它只在管理页面而不是前端显示脚本。

仅供参考:我正在构建一个插件来在 wordpress 中显示系统信息。这是为插件/主题作者提供有用的信息来解决用户报告的问题。

编辑:

简而言之,我需要一种方法来获取所有脚本和样式在管理和前端中排队,并在自定义管理页面中查看它们。

4

2 回答 2

6

样式和脚本都有一个专用的 Wordpress 动作钩子。这些钩子将在每个脚本或样式入队后触发:

function inspect_scripts() {
    global $wp_scripts;
    print_r($wp_scripts->queue);
}
add_action( 'wp_print_scripts', 'inspect_scripts' );

function inspect_styles() {
    global $wp_styles;
    print_r($wp_styles->queue);
}
add_action( 'wp_print_styles', 'inspect_styles' );
于 2016-02-06T09:15:34.317 回答
0

When I did <?php global $wp_scripts; var_dump($wp_scripts->queue); ?> on the index.php page of the standard 2014 Wordpress theme it showed me non-admin scripts that were being queued. If you just do var_dump on the $wp_scripts variable itself you'll get lots of scripts, I am guessing all the scripts in the theme or something like that.

So if you are only showing this info to the site administrator then it should be fine just to echo it out like at the bottom of the page. I used a memory profiling plugin once (it showed me load times/memory loads used by various plugins) and it put that information at the bottom of the page, it was pretty useful.

But like the comments say if you are only displaying the queued scripts then in the wp-admin area that is only the admin scripts, on the front end you'll get more.

于 2014-12-18T06:08:32.040 回答