Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the proper way to include a stylesheet with my options panels in wp-admin for a given plugin or theme. In other words -- the stylesheet that styles the panels.

I mean, in my panel template code, I'm doing something like...

<style type="text/css">
@import url("../wp-content/plugins/myplugin/panels/style.css");
</style>

...but suspect that this won't work on some installs of WordPress because of paths and so on, and might not work in the future of WP gets a wild hair and wants to rename wp-content with something else.

share|improve this question
    
    
@Sisir please post answers as answers, rather than as comments. –  Chip Bennett Apr 9 '12 at 18:45

2 Answers 2

up vote 1 down vote accepted

The best practice method of enqueueing admin stylesheets is to hook into your appearance page's specific hook, using the admin_print_styles-{hook}, where {hook} = {admin_page}-{menu_slug}.

I'll assume:

  1. This is a Theme
  2. You are properly using an appearance page, via add_appearance_page()
  3. Your $menu_slug is wpse48416-settings

The hook, then, is: admin_print_styles-appearance_page_wpse48416-settings:

<?php
function wpse48416_admin_style(){
        wp_register_style( 'wpse48416_admin_css', get_template_directory_uri() . '/css/admin.css' );
        wp_enqueue_style( 'wpse48416_admin_css' );
}
add_action( 'admin_print_styles-appearance_page_wpse48416-settings', 'wpse48416_admin_style' );
?>

For a Plugin, you simply need to use the correct {page} string, depending on which admin menu function you use to register your settings page, e.g. settings_page for add_settings_page().

share|improve this answer
    
What do I do if I'm using add_menu_page() and add_submenu_page() to add my admin panels? –  Volomike Apr 13 '12 at 21:39
    
"What do I do if I'm using add_menu_page() and add_submenu_page() to add my admin panels" - well, the best practice first step would be to switch to add_theme_page() or add_settings_page(). (grin). But, I think, for add_menu_page() or add_submenu_page(), you'd use admin_page instead of appearance_page. –  Chip Bennett Apr 13 '12 at 21:56
    
I found that they have some fantastic examples at the bottom of these pages to solve my problem, and I have it working! Yay! Go to codex.wordpress.org/Function_Reference, then search "wp_enqueue_script" and "wp_enqueue_style". –  Volomike Apr 13 '12 at 22:06
    
BTW, you said this was the best practice, but is it still the best practice? Note I just discovered this: codex.wordpress.org/Plugin_API/Action_Reference/… –  Volomike Apr 13 '12 at 22:32
    
function load_myplugin_wp_admin_style(){
        wp_register_style( 'myplugin_wp_admin_css', plugins_url( '/myplugin/panels/style.css' ), false, '1.0.0' );
        wp_enqueue_style( 'myplugin_wp_admin_css' );
}
add_action('admin_enqueue_scripts', 'load_myplugin_wp_admin_style');
share|improve this answer
    
While this method works, it will print your custom stylesheet on every admin page. At best, it represents bloat; at worst, it will cause style breakage for core and/or other Plugins. –  Chip Bennett Apr 9 '12 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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