How to check if WordPress sidebar has widgets?


#1

Does anyone know the best way to check if the user has put any widgets into a sidebar?

I always thought you could use is_sidebar_active() function to achieve this…

I’m trying to accomplish something like this:

if ( is_sidebar_active('page-sidebar') ) {

	dynamic_sidebar('page-sidebar');
		
} else {

	echo "

This is a sidebar, silly! Put some widgets in here!

"; }

However it seems that the is_sidebar_active() function returns true no matter if the sidebar has widgets or not. Anyone know a good way to accomplish this?


#2

Maybe is_active_sidebar function instead of is_sidebar_active?


#3
twi said

Maybe is_active_sidebar function instead of is_sidebar_active?

Wow… yup, you’re right. I’m need to take a nap now lol


#4

For your task is probably better is_dynamic_sidebar(), that check if there are active widgets in it.


#5
ParkerAndKent said

For your task is probably better is_dynamic_sidebar(), that check if there are active widgets in it.

Thanks, I’ll give that one a go, too. Looking at the codex, it looks like that function is meant just for exactly what I want to do.


#6

Off: Happy 3000 sales Mr. Blvd!


#7
glossycat said

Off: Happy 3000 sales Mr. Blvd!

Ha thanks, but I’ll hold the celebration until I finally get that Zoolander paw… a.k.a, Blue Steel!


#8

Have you found the solution? If not, this is how i do it and it works :slight_smile:

<?php if ( !function_exists('dynamic_sidebar')  || !dynamic_sidebar( 'Footer Widgets' ) )  ?>  
                

About

This is the 'Footer Widgets' widget section, add some widgets to it to change it.

<?php endif; ?>

#9

@dejans - Yeah, that’s the regular way of calling a sidebar, dunno if there’s a WordPress developer that doesn’t use that. I guess he’s trying to do something else.


#10
wpCanyonThemes said

@dejans - Yeah, that’s the regular way of calling a sidebar, dunno if there’s a WordPress developer that doesn’t use that. I guess he’s trying to do something else.

Yeah, all I was trying to do is just show a message in the couple different widget areas of the theme for when the user activates the theme for the first time to just help them get started… so they’re not looking at a completely blank page with header and footer. But my original code was exactly what I needed to do, I just had typed the function wrong as twi pointed out.


#11

Awesome, I was looking for a way to check if a specific sidebar panel had any widgets and this answered my question perfectly.


#12

is_active_sidebar() only works if you defined an actual sidebar id :::

Example from Codex :::

<?php $args = array( 'name' => sprintf(__('Sidebar %d'), $i ), 'id' => "sidebar-$i", 'before_widget' => '
  • ', 'after_widget' => '
  • ', 'before_title' => '

    ', 'after_title' => '

    ' ); ?>