How can I detect a widget in WP?

by Greg D
6 replies
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
#detect #widget
  • Profile picture of the author KirkMcD
    It would be easiest if your plugin just checked the wp database to see if the plugin is enabled.
    {{ DiscussionBoard.errors[2799643].message }}
    • Profile picture of the author mywebwork
      Originally Posted by KirkMcD View Post

      It would be easiest if your plugin just checked the wp database to see if the plugin is enabled.
      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
      {{ DiscussionBoard.errors[2799792].message }}
  • Profile picture of the author Greg D
    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
    Signature
    Premium Wordpress Directory Theme
    No Other Directory Theme Compares
    Premium Wordpress Adspace Plugin
    All On Site Advertising On Autopilot
    {{ DiscussionBoard.errors[2800052].message }}
  • Profile picture of the author SteveJohnson
    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.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2800581].message }}
  • Profile picture of the author Greg D
    Thank you Steve.

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

    Greg
    Signature
    Premium Wordpress Directory Theme
    No Other Directory Theme Compares
    Premium Wordpress Adspace Plugin
    All On Site Advertising On Autopilot
    {{ DiscussionBoard.errors[2802018].message }}
  • Profile picture of the author SteveJohnson
    you're welcome - let me know if you run into any more roadblocks.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2803707].message }}

Trending Topics