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.
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.
Please check this reference is_active_widget, check the example for an exact solution. That code will go within the
__construct()
method right afterparent::__construct()
is called.wp_enqueue_style
works if it is called beforewp_head
is called. andwidget()
method are called definitely afterwp_head
is loaded as it just display the HTML template.So here is an example of loading scripts for widget.