There are various approaches (including this one), but you could consider, since you’re talking about a stylesheet and not a script, simply enqueueing it from the Plugin, regardless of whether the Widget is active. (Which is worse: the few bytes of Widget-specific CSS, or the server overhead to determine if the Widget is active?)
Just add the following to your Plugin file (outside of the Widget class definition):
<?php
function plugin-name_print_stylesheet() {
?>
<style type="text/css">
/* CSS definitions go here */
</style>
<?php
}
add_action( 'wp_print_styles', 'plugin-name_print_stylesheet' );
?>
Or, if you’ve got a .css file with your Widget’s stylesheet:
<?php
function plugin-name_enqueue_stylesheet() {
wp_enqueue_style( 'plugin-name-stylesheet', plugins_dir( 'stylesheet.css', __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'plugin-name_enqueue_stylesheet' );
?>
p.s. don’t use a generic term such as my_slider
as a custom-function prefix. You should use your plugin-slug
instead, as a prefix for all of your Plugin’s custom functions.