Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to find a way to show a widget only if a given sidebar is empty. I'm using Widget Logic to control some widgets, but I'm having trouble figuring out how to use it in this case. I've tried to hook on to widget_logic_eval_override which seems like the way to go, but I'm not sure how to determine if the sidebar will be empty from inside that function.

Unfortunately, the widget I want to use is from a 3rd party plugin, and I'd rather not rewrite it. So, I can't easily accomplish this in the source for the widget.

Update: After looking at the answers and thinking about this more, I have a better understanding of the issue. There's no way to know whether or not a sidebar will be empty until after all of the widgets assigned to that sidebar have been "executed." So, while we are looping through widgets, we can't know if a fallback widget needs to be shown or not.

What could work would be to specify a given widget as a fallback. Then, when we're looping through widgets we don't display the fallback, but we save a reference to it in some global variable. Then, when we output the sidebar, if nothing is output, we access our global variable and show the fallback widget. Does that make sense? It seems like a really convoluted solution, so I'm wondering if there's a simpler way.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

If you want to display a fallback sidebar, you can do the following. If you want to decide which widget gets displayed, then idk.

Create 2 sidebars. Example: sidebar and fallback-sidebar.

In your template you can use:

if(is_active_sidebar( 'sidebar' )){
dynamic_sidebar( 'sidebar' );
} else {
dynamic_sidebar( 'fallback-sidebar' );
}

If you just trying to determine if a particular sidebar is active find the id of the sidebar and use:

if(is_active_sidebar( $sidebarID )){
//do stuff here
}

This might work: (Sorry im not at a pc i can test it at)

if(is_active_sidebar( $sidebarID )){
the_widget('WP_Widget_Archives', $instance, $args);
}
//I believe this will attempt to display the WP_Widget_Archives widget, if the
//sidebar with the id of $sidebarID is inactive.
share|improve this answer
    
The fallback sidebar is an interesting idea; I hadn't thought of that. I would rather use is_active_sidebar() though, but whenever I call that inside of a Widget Logic function I get a "Connection Reset" error from my browser. I get the feeling that it's causing an infinite loop somehow, but I'm not sure how. Any ideas on how to mitigate that? –  Dominic P Jul 8 '13 at 19:19
    
Sorry im not familiar with how Widget Logic Works. I would suggest trying to use a solution that reduces the use of additional plugins. Ive got one more idea i will edit my answer with it. –  Ronburgundy Jul 8 '13 at 19:38
    
the_widget() works perfectly. I never knew about that function. Thanks. –  Dominic P Jul 8 '13 at 20:00
    
Awsome! :D. Im hapy that worked. And thank you for my first accepted answer! :) cheers! –  Ronburgundy Jul 8 '13 at 20:03
add comment

You can determine if a sidebar is empty using,

is_dynamic_sidebar(); //returns boolean

There is another hacky solution here if you need to use widget logic since it will still be "active": http://stackoverflow.com/questions/2468813/wordpress-how-to-know-if-a-sidebar-is-empty-without-loading-it (not 100% sure it will work).

share|improve this answer
    
This seems to return true whether the sidebar is empty or not. It could be because WordPress thinks widgets have been displayed even if Widget Logic hides them? –  Dominic P Jul 8 '13 at 19:16
    
Yes widget logic is probably still active though hidden, typically you do this at the template level (using the_widget). Basically the drag and drop area in the admin has to be empty. I edited my answer for another possible solution though hacky. –  Wyck Jul 8 '13 at 19:17
    
the_widget() was definitely what I needed. Thanks for suggesting it. –  Dominic P Jul 8 '13 at 20:03
add comment

Based on this Answer (using custom function with Widget Logic) and this one (get number of widgets on a sidebar), I wrote the following code:

function check_my_widget_logic( $sidebar_index )
{
    global $_wp_sidebars_widgets;
    $index = "sidebar-{$sidebar_index}";
    if( empty( $_wp_sidebars_widgets[ $index ] ) )
        return true;

    return false;
}

Usage in Widget Logic: check_my_widget_logic(3). Use the ID of the target sidebar as the function parameter (3 in this example).

Can be found inspecting the Widget in admin:

enter image description here

share|improve this answer
    
This is a nice idea, but I think it will run into the same problem that Wyck's answer does. Even if the widgets output nothing, this function will return false and the fallback widget won't be shown. I haven't tested it yet, but I'm pretty sure that empty( $_wp_sidebars_widgets[ $index ] ) is always going to evaluate as true when we have widgets assigned to a sidebar (whether they output content or not). –  Dominic P Jul 8 '13 at 19:35
    
I'm trying to find a way to show a widget only if a given sidebar is empty. - I tested and it works. –  brasofilo Jul 8 '13 at 19:38
    
Just tested, and the fallback isn't showing for me. I probably should have been more clear about what I meant by "empty." I updated the question to hopefully clarify things. –  Dominic P Jul 8 '13 at 19:50
    
Glad you got it working. And thanks for prompting this snippet, went straight to the code library ;) –  brasofilo Jul 8 '13 at 20:10
    
What is the code library? –  Ronburgundy Jul 10 '13 at 14:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.