I have create a custom widget for Elementor. I want to check with the code whether my widget is used or loaded (active) on the page or not? How can I do this? I want the styles related to the widget to be loaded or enqueue style if my widget was used on the page.

I wrote the following code but it didn't work:

function load_custom_elementor_widget_styles() {
    if ( \Elementor\Plugin::$instance->widgets_manager->is_widget_active( 'my_widget_id' ) ) {
        wp_enqueue_style( 'my-widget-styles', plugin_dir_url( __FILE__ ) . 'path/to/your/widget-styles.css' );
add_action( 'wp_enqueue_scripts', 'load_custom_elementor_widget_styles' );

This condition does not work. Do you have a solution?

if ( \Elementor\Plugin::$instance->widgets_manager->is_widget_active( 'my_widget_id' ) ){}
  • Just going to go straight for the obvious here is 'my_widget_id' actually the id of the widget? That is the first thing I would check. If so then perhaps the widget manager might not be initialised yet at the point where you are checking for the widget activation. In that case, you can try hooking your function to a later action, such as "wp_loaded" or "wp_head" Apr 14, 2023 at 9:47
  • @JaimeePage I found the solution. The Elementor plugin itself has provided a solution that you can use in your class
    – alex dev
    Apr 14, 2023 at 16:50


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.