Você pode usar a função is_active_widget . Por exemplo:
function check_widget() {
if( is_active_widget( '', '', 'search' ) ) { // check if search widget is used
wp_enqueue_script('my-script');
}
}
add_action( 'init', 'check_widget' );
Para carregar o script na página em que o widget é carregado apenas, você terá que adicionar o código is_active_widget () na sua classe widget. Por exemplo, veja o widget de comentários recentes padrão (wp-includes / default-widgets.php, linha 602):
class WP_Widget_Recent_Comments extends WP_Widget {
function WP_Widget_Recent_Comments() {
$widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
$this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops);
$this->alt_option_name = 'widget_recent_comments';
if ( is_active_widget(false, false, $this->id_base) )
add_action( 'wp_head', array(&$this, 'recent_comments_style') );
add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
}