How can I detect a widget in WP?

by 6 replies
8
Hi all,

I am trying to find a way to detect a widget in the sidebar from a plugin.

I want my plugin to be able to detect if a certain widget is installed or not installed from within my plugin not the theme.

Any suggestions?

Thank you.

Greg
#programming #detect #widget
  • It would be easiest if your plugin just checked the wp database to see if the plugin is enabled.
    • [ 1 ] Thanks
    • [1] reply
    • Hi Kirk

      I've been working with Greg and although your answer is appreciated I think some clarification is in order.

      The issue is not detecting if the plugin is enabled. The issue is detecting if a widget from the plugin has been dragged to one of the widget-enabled sidebars.

      WordPress has an "is_active_widget" function but it doesn't seem to work if called within the plugin - I can get it to work within the theme but thats pointless in this case. I've literally spent hours looking through the database, best I can do so far is detect that the widget was at one point dragged into a sidebar. However I can't yet determine if it has been removed.

      Any thoughts?

      Thanks

      Bill
      • [ 1 ] Thanks
  • I also appreciate the help Kirk.
    Sorry for the misleading question, I wanted to help Bill.
    Thank you Bill for clarifying.

    I hope Kirk or someone can help shed some light.

    Greg
  • Bill, is_active_widget() doesn't work in your plugin because your plugin is loaded before widgets are initialized. You have to hook your detection function into the load sequence after widgets_init action, say wp_head:

    PHP Code:
    function my_widgie_detect()  {
      if ( 
    false === is_active_widget'widgie_name' )
        
    // do something here if the widget isn't active;
      
    else
        
    // do something here;
    }
    add_action'wp_head''my_widgie_detect' ); 
    Edit: this is assuming that you're working on a page load. If you're working on an admin page, you'll want to hook into somewhere around admin_init, still after widgets have initialized.

    Alternatively, you can always get the sidebars_widgets option from the options table and look through it. You'll have to check all of the active sidebars for the widget, but that's not a big stretch.
    • [ 2 ] Thanks
  • Thank you Steve.

    This has helped put this on the right track. I appreciate the input.

    Greg
  • you're welcome - let me know if you run into any more roadblocks.

Next Topics on Trending Feed