CSS and Javascript Scripts for Widgets can be used based on widget availability. If you have created a custom widget and need to load stylesheet only if it active on front end, you will have to use is_active_widget WordPress function right after parent::__construct
method has been called.
class MY_Custom_Widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'my_custom_widget',
'description' => __( 'MY Custom Widget' )
);
parent::__construct(
'my_custom_widget',
__( 'My Custom Widget' ),
$widget_ops
);
}
// rest of the widget codes
}
Now for using Scripts for Widget outside of the Widget class, ex: loading stylesheet for calendar
widget, you just need to figure out the widget unique id.
function w4dev_load_calendar_scripts() {
if ( is_active_widget( false, false, 'calendar', true ) ) {
wp_enqueue_script( 'jquery' );
wp_enqueue_style( 'something', 'stylesheet_url' );
}
}
add_action( 'wp_head', 'w4dev_load_calendar_scripts');