您可以使用$pagenow
全局变量,它显然会返回widgets.php
在小部件破折号中时。
如果要按路径将CSS/JS排队:
add_action( "admin_enqueue_scripts", "wpse_250923_enqueue_scripts" );
function wpse_250923_enqueue_scripts() {
global $pagenow;
// check for widgets
if ( "widgets.php" !== $pagenow )
return; // nothing to do here
// enqueue CSS
wp_enqueue_style( "my-css-handle", "path/to/some/css/file.css" );
// enqueue JS
wp_enqueue_script( "my-js-handle", "path/to/some/js/file.js", array(\'jquery\') /*dependencies*/ );
}
否则,如果您只有一些内联JavaScript和/或内联CSS(未存储在文件中),则分配
admin_head
对于CSS和
admin_footer
对于JS:
add_action( "admin_init", "wpse_250923_enqueue_scripts" );
function wpse_250923_enqueue_scripts() {
global $pagenow;
// check for widgets
if ( "widgets.php" !== $pagenow )
return; // nothing to do here
// enqueue CSS
add_action( "admin_head", "wpse_250923_print_css" );
// enqueue JS
add_action( "admin_footer", "wpse_250923_print_js" );
}
function wpse_250923_print_css() {
print \'<style>/*CSS tweaks*/</style>\' . PHP_EOL;
}
function wpse_250923_print_js() {
print \'<script type="text/javascript">/*JS goes here*/</script>\' . PHP_EOL;
}
希望这有帮助。提前新年快乐!