0

I am trying to put a custom inline JavaScript is an options page created by a plugin.
The page URL is like this wp-admin/admin.php?page=psbg.

This is the code I am using to output my script at the head.

function add_scripts() {
    $current_screen = get_current_screen();

    if ( $current_screen = 'admin.php?page=psbg' ) {
        // My codes here
    }
}
add_action( 'admin_head', 'add_scripts' );

That's not working. But if I change the compare value of $current_screen to

if ( $current_screen = 'admin.php' ) {
    // My codes here
}

it works, but adds the code to every page with the parent page of admin.php.

What I am doing wrong?

2 Answers 2

4

There are several ways to add scripts only to certain admin pages. The best option, from my point of view, is to hook into admin_enqueue_scripts and use the $hook parameter:

add_action('admin_enqueue_scripts', 'cyb_admin_scripts');
function cyb_admin_scripts( $hook ) {

    //You may need adjustment here to match for page
    //If the URL of your page is admin.php?page=psbg, the hook
    // to check would be toplevel_page_psbg
    if( $hook == 'toplevel_page_my-page-slug' ) {

        wp_register_script('my-script', plugins_url() . '/my-plugin/js/script.js', array('jquery'));
        wp_register_style('my-style', plugins_url() . '/my-plugin/css/style.css');
        wp_enqueue_style('my-style');
        wp_enqueue_script( 'my-script');

    }

}

Other common way I see often is using the load-page action to enqueue the files. For example:

//Adding the menu in admin area
add_action('admin_menu', 'cyb_add_menu');
function cyb_add_menu(){

    $hook_suffix = add_menu_page(__('Page title'), __('Menu title'), 'manage_options', 'my-page-slug', 'display_callback_fn');
    // Call to cyb_settings_page_load() when our page is loaded
    add_action( 'load-'. $hook_suffix, 'cyb_settings_page_load' );  

}

function cyb_settings_page_load() {
    //Enqueue styles and scripts
    wp_enqueue_script('my-script', plugins_url() . '/my-plugin/js/script.js', array('jquery'));
    wp_enqueue_style('my-style', plugins_url() . '/my-plugin/css/style.css');
}

function display_callback_fn() {
    //Build your page hiere
}
6
  • I really appreciate your help, I really do, but none of them worked in my case.
    – Abhik
    Aug 25, 2014 at 9:56
  • Really? I've tested the code before posting and it works perfectly. Have you verify the variables values? You have to modify some of them to match your page. See comments in the code I posted in the answer.
    – cybmeta
    Aug 25, 2014 at 10:00
  • I did mate, the second option isn't for me as I am trying to put code at a page created by another plugin.. And, at the first option, I changed the if( $hook == 'toplevel_page_my-page-slug' ) { to if( $hook == 'toplevel_page_psbg' ) {. But not sure why that didn't work.
    – Abhik
    Aug 25, 2014 at 10:04
  • I've created the page wp-admin/admin.php?page=psbg and tested the code again and it works checking if( $hook == 'toplevel_page_psbg' ) . Can you make a var_dump($hook) inside cyb_admin_scripts() function to see the value you get in wp-admin/admin.php?page=psbg?
    – cybmeta
    Aug 25, 2014 at 10:10
  • string(19) null awesomescreenshot.com/0bc3d98s6c
    – Abhik
    Aug 25, 2014 at 10:18
-1

After a simple print_r of the get_current_screen(); reveled what I need.

This is what worked..

function add_scripts() {
    $current = get_current_screen();

    if ( $screen->parent_base == 'psbg' ) {
        // My codes here
    }
}
add_action( 'admin_head', 'add_scripts' );
3
  • I'm sorry to downvote but scripts in admin area should be registered/added using admin_enqueue_scripts action hook.
    – cybmeta
    Aug 25, 2014 at 10:00
  • @cybmeta No, its not, specially when I have only couple of lines of JavaScript. Can you show me any documentation about that? And, no problem for the downvote.
    – Abhik
    Aug 25, 2014 at 10:06
  • The codex is a good documentation to start: codex.wordpress.org/Plugin_API/Action_Reference/… There are some questions and answers in this site about this issue. For example wordpress.stackexchange.com/questions/112352/… It doesn't matter how many lines of code you have, the action hook admin_enqueue_scripts is designed to enqueue scripts and styles in admin area and it the one that should be used for that purpose.
    – cybmeta
    Aug 25, 2014 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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