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.

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

1 Answer 1

up vote 1 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

 
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.