0

I am using this code to enqueue scripts and styles to wordpress plugin. This works well, but I am wondering is there some parameter available in my_admin_enqueue_scripts function? Because I would like to enqueue different scripts per page. I know I can call different function like my_admin_enqueue_scripts, my_admin_enqueue_scripts2, my_admin_enqueue_scripts3 but I would still need to repeat many scripts to enqueue so I would like to know if I can detect on what page I am in my_admin_enqueue_scripts function.

    add_action("admin_menu", "my_admin_menu");

    function my_admin_menu(){

        $menu = add_menu_page('Audio Player', 'Audio Player', MY_CAPABILITY, 'my_settings', 'my_settings_page', 'dashicons-playlist-audio');

        $submenu = add_submenu_page('my_settings', __('Audio Player', MY_TEXTDOMAIN), __('Settings', MY_TEXTDOMAIN), MY_CAPABILITY, 'my_settings', 'my_settings_page'); 
        $submenu2 = add_submenu_page('my_settings', __('Audio Player', MY_TEXTDOMAIN), __('Player manager', MY_TEXTDOMAIN), MY_CAPABILITY, 'my_player_manager', 'my_player_manager_page');  

        add_action( 'load-' . $menu, 'my_admin_enqueue_scripts' );
        add_action( 'load-' . $submenu, 'my_admin_enqueue_scripts' );
        add_action( 'load-' . $submenu2, 'my_admin_enqueue_scripts' );

    }


    function my_admin_enqueue_scripts($param) {

        //is there a $param here?

        wp_enqueue_script('jquery');
        wp_enqueue_script('jquery-ui-sortable');
        wp_enqueue_media();

        wp_enqueue_style("spectrum", plugins_url('/css/spectrum.css', __FILE__));
        wp_enqueue_script("spectrum", plugins_url('/js/spectrum.js', __FILE__), array('jquery'));   


            .... other scripts


    }
1

//is there a $param here?

None, the hook you're using does not pass any parameters to the callback.

But you can instead use the admin_enqueue_scripts hook which passes the hook name for the current menu page — the hook name is also saved in the global $hook_suffix variable. You can then use get_plugin_page_hookname() to get the hook name for a specific menu page and then conditionally enqueue the scripts for your menu pages.

function my_admin_enqueue_scripts( $hook_suffix ) {
    switch ( $hook_suffix ) {
        // Enqueue scripts for the "Audio Player -> Settings" page.
        case get_plugin_page_hookname( 'my_settings', '' ) :
            //wp_enqueue_script( ... );
        break;

        // Enqueue scripts for the "Audio Player -> Player manager" page.
        case get_plugin_page_hookname( 'my_player_manager', 'my_settings' ) :
            //wp_enqueue_script( ... );
        break;
    }
}
add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );

And actually, you should use the admin_enqueue_scripts hook for enqueueing admin scripts. :)

Additionally, in your first add_submenu_page() call, you should just omit the sixth parameter (i.e. the callback) than setting it to my_settings_page, to prevent the function from being called twice on the same page.

| improve this answer | |

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.