0

I am creating a custom theme with some widgets. In the theme, to make it easier for my client to update, I am using SiteOrigin's Page Builder plugin, which uses widgets to display content. I am creating an events list widget which will work with a custom post type. I want the widget to display a map of the area, but ONLY if it ISN'T in my sidebar (the sidebar is the only place I have declared widgets. If it is in my sidebar, it won't load the map.

I can't seem to find any solutions after a cursory look on Google. Most suggest using CSS to hide / show elements. I was hoping there was something in WP 4.1 that would check if a widget was inside a particular sidebar. Could anyone help?

1
  • Can you work with a solution that checks the number of widgets in the sidebar supported by an if-else statement? i.e. If # of widgets exceeds x, do that else do that? I have that snippet ready.
    – RaajTram
    CommentedJun 7, 2016 at 1:41

2 Answers 2

1

You should make the check inside the widget class, unless you don't have a choice. The example in the codex is pretty much what you're looking for:

class cbs_map_widget extends WP_Widget{ function cbs_map_widget(){ $this->WP_Widget('cbsmaps', __('Widget Name'), array('classname' => 'cbsmaps', 'description' => __('whatever'))); ... add_action('wp_enqueue_scripts', array(&$this, 'js')); } function js(){ if ( is_active_widget(false, false, $this->id_base, true) ) { // enqueue your scripts; } } ... } 

The $widget_id parameter is useful when you need to include your scripts only for a specific widget instance. For example when you have two cbs_map_widget widgets and want to include custom javascript for each of them.

The $callback argument is useful when you need to do the check outside the widget class (like you did above), but try to avoid it if you can do your check inside the class.

The other arguments are pretty much self explanatory (id_base is basically a "widget class indentifier", and the $skip_inactive is whether to check inactive widgets as well - should be true because I don't think you want to do that)...

Again, you don't need this unless you specifically want to check if a certain widget instance is active. Personally I use this method because I can access the instance options too:

... function js(){ // we need to process all instances because this function gets to run only once $widget_settings = get_option($this->option_name); foreach((array)$widget_settings as $instance => $options){ // identify instance $id = "{$this->id_base}-{$instance}"; // check if it's our instance if(!is_active_widget(false, $id, $this->id_base)) continue; // not active // instance is active // access the widget instance options here, you may need them to do your checks if($options['yourwidgetoption']) { // do stuff } } } ... 
    0

    WordPress has a function for that already. is_active_widget As the codex points out you must use the function after the widgets have all loaded. Then test the conditions with an if statement and if the map widget is not loaded you can run the rest of your script to load the map in your template.

    http://codex.wordpress.org/Function_Reference/is_active_widget

    2
    • I think I might not have explained myself properly. is_active_widget() seems to show if the widget is active in the sidebar. It seems to disregard if this instance of the widget is in the sidebar. I have used echo is_active_widget(false, false, $this->id_base, false); and it returns the sidebar id on all instances where the widget is used. I just want to see if this instance is there.
      – Nate
      CommentedFeb 22, 2015 at 21:12
    • I have also tried with $this->widget_id but it returns no value. $id_base seems to be the only one to return something.
      – Nate
      CommentedFeb 22, 2015 at 21:15

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.