0

I am trying to style my plugin's admin page. Below is the code is use, but neither the stylesheet nor the script shows up on my page.

add_action( 'admin_init', 'myplugin_admin_init' );
add_action( 'admin_menu', 'myplugin_custom_menu_page' );

function myplugin_custom_menu_page(){
    $page = add_menu_page( 'My Plugin', 'My Plugin', 'manage_options', 'myplugin/myplugin-admin.php', '', plugins_url( 'myplugin/images/icon.png' ), 33 );
     add_action( 'admin_enqueue_scripts' . $page, 'myplugin_admin_styles' );
     add_action( 'admin_enqueue_scripts' . $page, 'myplugin_admin_scripts' );
}

function myplugin_admin_init() {
  wp_register_script( 'myplugin-script', plugins_url( 'myplugin/script.js') );
  wp_register_style( 'myplugin-style', plugins_url('myplugin/stylesheet.css') );
}

function myplugin_admin_styles() {
    wp_enqueue_style( 'myplugin-style' );
}

function myplugin_admin_scripts() {
    wp_enqueue_script( 'myplugin-script' );
}

Any hints would be much appreciated!

1 Answer 1

1

An alternative way to add admin scripts/styles only on your plugin's page:

//The add_action to add onto the WordPress menu.
add_action('admin_menu', 'myplugin_custom_menu_page');
function myplugin_custom_menu_page() {
    $GLOBALS['my_plugin_page'] = add_menu_page( 'My Plugin', 'My Plugin', 'manage_options', 'myplugin/myplugin-admin.php', '', plugins_url( 'myplugin/images/icon.png' ), 33 );
}

add_action( 'admin_enqueue_scripts', 'my_enqueue_scripts' );
function my_enqueue_scripts($hook_suffix) {
    if( $GLOBALS['my_plugin_page'] == $hook_suffix ) {
        wp_enqueue_style( 'myplugin-admin', plugins_url('stylesheet.css', __FILE__) );
        wp_enqueue_script( 'myplugin-admin', plugins_url('script.js', __FILE__) ); 
    }
}

In this approach, you don't have to pay attention to screen name. You will always enqueue your scripts for your plugin's page only.

5
  • why did you edit my answer just to add code styles instead of quotes?
    – JMau
    Aug 26, 2013 at 10:40
  • @JMau in this way it is easier to read inline quotation. Aug 26, 2013 at 10:59
  • Ok then. Your answer could be easier to implement for @eevaa. Still I think this is quite more logical to use admin_print_style even if the codex is telling, for the general case, use admin_enqueue_scripts
    – JMau
    Aug 26, 2013 at 11:05
  • This does not work for me either. These are basically just variations of what I did and since I can't see any apparent errors in any of the solutions, the problem must be somewhere else..
    – eevaa
    Aug 26, 2013 at 13:13
  • @eevaa I have updated the snippet, there was an error, which has been fixed. Aug 26, 2013 at 13:22

Your Answer

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

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