Registering a stylesheet inside my WordPress widget

I did see the other topic similar to this but I cannot get mine to work.

I am using WordPress 3.8 and am creating a custom widget for a site. I need to style it a certain way so I need to load an external stylesheet.

Read More

I have tried:

function widget($args, $instance)
  {
    wp_enqueue_style( 'gamestats-style', plugins_url('css/styles.css', __FILE__) ); 

    //Widget content
  }

with no luck. I have the css file located in the css directory of my plugin directory “game-stats” so i believe the url is correct.

plugins/game-stats/css/styles.css

if I embed the styles in the php by way of <script>....styles...</script> it works fine but this is really bad practice. What am I doing wrong?

Any help would be greatly appreciated.

Related posts

1 comment

  1. Please check this reference is_active_widget, check the example for an exact solution. That code will go within the __construct() method right after parent::__construct() is called. wp_enqueue_style works if it is called before wp_head is called. and widget() method are called definitely after wp_head is loaded as it just display the HTML template.

    So here is an example of loading scripts for widget.

Comments are closed.