Home>Support>list of active widgets?

list of active widgets?

By cdillon, 10 years ago. Last reply by cdillon, 10 years ago.

Is there a way to get a list of widgets used? I have a plugin that conditionally loads styles and scripts when a widget is active using the the WordPress

is_active_widget
function, but that returns false since the widget is not in a sidebar.

And/or is there a global variable to indicate when we’re on a Page Builder page?

Thanks,
Chris

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 10 years, 22 days ago Greg Priday
    Hi, I Work Here

    Hi Chris

    There are 2 ways you can handle this. The way I do it with my widgets is to enqueue the scripts and styles in the actual widget function. Page Builder pre calls all these functions before the header (I guess this would be pre rendering) so all your scripts and styles will be available in the head.

    Another option is to use the would be to use something like the following.

    function my_plugin_enqueue_scripts(){
    	$panels_data = get_post_meta( get_the_ID(), 'panels_data', true );
    	if(empty($panels_data['widgets'])) return;
    	
    	foreach( $panels_data['widgets'] as $widget ) {
    		if($widget['info']['class'] == 'MyWidget_Class') {
    			// Enqueue scripts here
    			break;
    		}
    	}
    }
    add_action('wp_enqueue_scripts', 'my_plugin_enqueue_scripts');
    

    I haven’t double checked that code, but I’m fairly sure it’ll all work. At worst, just use a var_dump for $panels_data to double check what’s all in there.

  2. 10 years, 22 days ago cdillon

    Thanks, Greg. That function works well as a Page Builder equivalent of `is_active_widget`.

    I am loading the scripts in the widget->display and I’m not sure why I was checking is_active_widget there so removing that helped.

    But the real problem was that I was registering the scripts and styles in my plugin setup, then enqueueing them in the widget(s). Page Builder’s preloading seemingly happens before `wp_enqueue_scripts` so the scripts were not registered and therefore could not be enqueued and localized. Enqueing them entirely in the widget->display solves it.

    My plugin is Strong Testimonials (http://wordpress.org/plugins/strong-testimonials/). After some more testing, I will add Page Builder to my 100% Compatible list.

    Thanks for your help. Cheers on a great plugin!

    Chris

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More