I'm looking for a solution to check is_active_widget on particular page.
I got this https://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-particular-shortcode-or-widget-is-present
However the second way of sorich87 still loads the the scripts of a widget on every page where there's no certain active widget on the running/active page. Even when the sidebar of each page has different id & name.
For example:
I have page 'A' (with sidebar 'A1') which consists 'X' active widget and x_scripts in it.
Then when I check on page 'B' (with sidebar 'B1') which doesnt consist 'X' widget, it still loads the x_scripts of 'X' widget. Seems that this is_active_widget function only check for general activated widget, not on the running page.
This is my code:
class X extends WP_Widget
{
function X(){
$widget_ops = array('description' => 'X widget');
$control_ops = array('width' => 200, 'height' => 500);
parent::WP_Widget(false,$name='X-widget',$widget_ops,$control_ops);
if ( is_active_widget(false, false, $this->id_base, true) ){
add_action('wp_head', 'x_styles',0);
add_action( 'wp_footer', 'x_scripts' );
}
}
//rest of functions here
}
How to prevent this?