1

Possible Duplicate:
Where is the right place to register/enqueue scripts & styles

When I enqueue a css style in a plugin like so:

function add_my_stylesheet() 
{
    wp_enqueue_style( 'myCSS', plugins_url( '/css/myCSS.css', __FILE__ ) );
}

add_action('init', 'add_my_stylesheet');

Is 'init' the correct action to use? It works fine. Is there any reason not to do it this way? Is this the standard way? I want to follow the best practices.

Thank you.

-Laxmidi

4
  • Do you want to enqueue style on plugin's options page?
    – Mamaduka
    Commented Sep 3, 2011 at 7:11
  • if you only have 1 wp_enqueue_style, just add it directly in your plugin, no need to add any function or add_action.
    – Sean Lee
    Commented Sep 3, 2011 at 17:31
  • @Sean Lee bad advice. It is strongly recommended to enqueue only inside appropriate hooks. It is also commonly recommended to not run any code during plugin load, other than hooking function/methods for executing code later.
    – Rarst
    Commented Sep 5, 2011 at 10:52
  • Agree with you Rarst in general, especially when there are multiple of them.
    – Sean Lee
    Commented Sep 6, 2011 at 3:27

1 Answer 1

2

You can use admin_print_styles for your hook, this is the preferred method for adding a style to your plugin or admin page.

add_action('admin_print_styles', 'add_my_stylesheet');
2
  • 1
    Why WP_PLUGIN_URL and not plugins_url()? EDIT: Just reading here (codex.wordpress.org/Determining_Plugin_and_Content_Directories) that the contants are intended for core use, not plugins.
    – tollmanz
    Commented Sep 5, 2011 at 5:01
  • You're right, not just for SSL but if the plugin is not in the default plugins directory, removing it.
    – Wyck
    Commented Sep 5, 2011 at 5:50

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