WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing a WordPress widget following Dave Clements tutorial. It works well. Now I want to add some styles to it. I want the styles to be in an extra css file which will be loaded during runtime. I'm calling this function

function myprefix_add_my_stylesheet() {
  wp_register_style( 'myprefix-style', plugins_url('mystyle.css', __FILE__) );
  wp_enqueue_style( 'myprefix-style' );

right before (see Dave's tutorial) "// Widget output //" using the following statement.

add_action( 'wp_enqueue_scripts', 'myprefix_add_my_stylesheet' );

But nothing seems to happen. What am I doing wrong?

share|improve this question
up vote 2 down vote accepted

wp_enqueue_scripts is called way before WordPress processes the widget content, so just as in this post, you've missed the boat :).

Instead, just call wp_enqueue_style directly:

function widget($args, $instance) {
    wp_enqueue_style( 'myprefix-style', plugins_url('mystyle.css', __FILE__) );

    //Widget content

(no need to register if you're just going to enqueue it straight after). Same works for wp_enqueue_script.

share|improve this answer
Stephen, thank you for the correct answer! – twigmac Jun 27 '12 at 17:46
No problem! Please mark accepted answers as correct by clicking the tick to the left. It takes your question off the unanswered pile and helps others find the solution. – Stephen Harris Jun 27 '12 at 18:26

Your Answer


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.