我只在插件页面上使用load将脚本排队。
https://developer.wordpress.org/reference/hooks/load-page_hook/
add_action("admin_menu", "sap_admin_menu");
function sap_admin_menu(){
$menu = add_menu_page("Sticky Audio Player Player manager", "Sticky Audio Player", "manage_options", "sap_player_manager", "sap_player_manager_page", \'dashicons-playlist-audio\');
$submenu = add_submenu_page("sap_player_manager", "Sticky Audio Player Player manager", "Player manager", "manage_options", \'sap_player_manager\', "sap_player_manager_page");
$submenu2 = add_submenu_page("sap_player_manager", "Sticky Audio Player Playlist manager", "Playlist manager", "manage_options", \'sap_playlist_manager\', \'sap_playlist_manager_page\');
add_action( \'load-\' . $menu, \'sap_admin_enqueue_scripts\' );
add_action( \'load-\' . $submenu, \'sap_admin_enqueue_scripts\' );
add_action( \'load-\' . $submenu2, \'sap_admin_enqueue_scripts\' );
}
这很好,但是我想知道如何检测我当前在哪个页面上,以便我可以在sap\\u admin\\u enqueue\\u scripts函数中将不同的脚本排队:
function sap_admin_enqueue_scripts() {
//I need to detect page here
if ( is_page( \'sap_player_manager\' ) ) {//this doesnt work
}
我尝试使用is\\U页面,但无法正常工作。
SO网友:Nuno Sarmento
你可以这样做
add_action( \'admin_menu\', \'my_admin_menus\' );
function my_admin_menus() {
$GLOBALS[\'my_page\'] = add_menu_page( \'Page Title\', \'Menu Title\',
MY_ADMIN_CAPABILITY, \'menu-slug\', \'show_page_content\');
}
add_action( \'admin_enqueue_scripts\', \'enqueue_admin_js\');
function enqueue_admin_js($hook) {
if($GLOBALS[\'my_page\'] === $hook) {
wp_enqueue_script( \'jquery-ui-core\' );
wp_enqueue_script( \'jquery-ui-tabs\' );
// Isn\'t it nice to use dependencies and the already registered core js files?
wp_enqueue_script( \'my-script\', INCLUDES_URI . \'/js/my_script.js\', array( \'jquery-ui-core\', \'jquery-ui-tabs\' ) );
}
}