0

I am using load to enqueue scripts only on my plugin page.

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' );

}

This works well, however I would like to know how to detect on which page I am currently so I can enqueue different scripts in sap_admin_enqueue_scripts function:

function sap_admin_enqueue_scripts() {

    //I need to detect page here

    if ( is_page( 'sap_player_manager' ) ) {//this doesnt work


    }

I tried using is_page but I cant make it work.

  • is_page is a frontend function, and works off of the main query – Tom J Nowell Aug 5 '17 at 18:32
2

You can do something like this

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' ) );
  }
}
  • It's great to use the action-hook argument but I would not use a global here, why not use the result of var_dump( $hook ), there is very very very little chance you need to change that slug later. Seems acceptable to me to use it as a simple string in if condition. – JMau Aug 5 '17 at 21:54
1

What about get_current_screen()?

$screen = get_current_screen();

if ( 'sap_player_manager' === $screen->id ) {

}
1

look into get_current_screen();

Some notes from codex:

This function is defined on most admin pages, but not all. Thus there are cases where is_admin() will return true, but attempting to call get_current_screen() will result in a fatal error because it is not defined. One known example is wp-admin/customize.php.

There is also a current_screen hook for a different approach.

Both make available a WP_Screen. in example:

WP_Screen Object {
    ["action"] => string(0) ""
    ["base"] => string(4) "post"
    ["columns":"WP_Screen":private] => int(0)
    ["id"] => string(12) "someposttype"
    ["in_admin":protected] => string(4) "site"
    ["is_network"] => bool(false)
    ["is_user"] => bool(false)
    ["parent_base"] => NULL
    ["parent_file"] => NULL
    ["post_type"] => string(12) "someposttype"
    ["taxonomy"] => string(0) ""
    ["_help_tabs":"WP_Screen":private] => array(0) { }
    ["_help_sidebar":"WP_Screen":private] => string(0) ""
    ["_options":"WP_Screen":private] => array(0) { }
    ["_show_screen_options":"WP_Screen":private] => NULL
    ["_screen_settings":"WP_Screen":private] => NULL
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.