Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I insert this conditional inside my widgetized area? WordPress

  • SOLVED

For my [[LINK href="http://darrenhoyt.com/products/gravy/"]]Gravy[[/LINK]] theme, I'd like to insert some optional widgets in the footer. And I need all of them wrapped in a bounding div ("footer-widgets"), but <strong>only</strong> if widgets are activated.

In theory it would be something like this:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widgets') ) : ?>
<div id="footer-widgets"><!--widget stuff--></div>
<?php endif; ?>


But that obviously doesn't work. Any ways to do this with minimal code?

Answers (10)

2009-12-12

Justin Tadlock answers:

Do you know the ID (not the name) of the widget area? That would work better.

The code should look like this:

<?php if ( is_active_sidebar( 'Footer Widgets' ) ) { ?>
<div id="footer-widgets">
<?php dynamic_sidebar( 'Footer Widgets' ); ?>
</div>
<?php } ?>


I'd replace "Footer Widgets" in that first line with the sidebar ID rather than the name, but it may work either way.

2009-12-14

Ben answers:

As people have mentioned above, is_active_sidebar($widgetId) works but you also have to remember to register your sidebar with an id.

Example:

register_sidebar(array(
'name' => 'sidebar-common',
'id' => 'sidebar-common',
'before_widget' => "\n\t\t\t" . '<li class="widget %2$s">',
'after_widget' => "\n\t\t\t</li>\n",
'before_title' => "\n\t\t\t\t". '<h3 class="widgettitle">',
'after_title' => "</h3>\n"
));


The id can be anything, from an integer to a full string.

2009-12-12

Sébastien Méric answers:

hi,

you should try something mike this :

copy/paste this in your function.php :

// Register widget area(s)
if ( function_exists('register_sidebar') ){
$sidebars = array(
'Widget area for the footer',
'another widget area'
);
foreach($sidebars as $sidebar):
register_sidebar(array(
'name'=>$sidebar,
'before_widget' => '<li id="%1$s" class="widget widget-%2$s">',
'after_widget' => '</li>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
endforeach;
}

and this in your footer.php :

<?php if ( function_exists( 'is_active_sidebar' ) && is_active_sidebar( 'Widget area for the footer' ) ) { ?>
<div id="footer-widgets">
<?php if ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Widget area for the footer' ) ) { ?>
<!--widget stuff-->
<?php } ?>
</div>
<?php } ?>

2009-12-12

Sébastien Méric answers:

sorry, you must change the div with a ul in this case...

<?php if ( function_exists( 'is_active_sidebar' ) && is_active_sidebar( 'Widget area for the footer' ) ) { ?>
<ul id="footer-widgets">
<?php if ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Widget area for the footer' ) ) { ?>
<!--widget stuff-->
<?php } ?>
</ul>
<?php } ?>

2009-12-13

Utkarsh Kukreti answers:

Try this code
<?php
if ( function_exists('dynamic_sidebar') && function_exists( 'is_active_sidebar' ) && is_active_sidebar( 'Footer Widgets' )) :
?>

<div id="footer-widgets">
<?php dynamic_sidebar( 'Footer Widgets' ) ?>
</div>

<?php endif; ?>

2009-12-14

Utkarsh Kukreti answers:

Do you know the ID of the widget?
The is_active_widget function only supports the ID of the widget, not by the name.

This is the code
<?php
$widget_id = 1;
if ( function_exists('dynamic_sidebar') && function_exists( 'is_active_widget' ) && is_active_widget( false, $widget_id ) !== false ) :
?>
<div id="footer-widgets">
<?php dynamic_sidebar( $widget_id ); ?>
</div>
<?php endif; ?>

2009-12-14

Bill Hunt answers:

How about this?


<? global $wp_registered_widgets;
if ( isset($wp_registered_widgets) && count( (array) $wp_registered_widgets ) ) { ?>
<!-- widget stuff goes here -->
<? endif; /* if isset widgets */ ?>

2009-12-14

Max answers:

Hello,

Wordpress mantains global arrays of variables that contains a lot of useful informations.
There are also a lot of accessible less documented functions.

You can access widgets information with:
$wp_registered_sidebars
$wp_registered_widgets


I think that the right function for you is
wp_get_sidebars_widgets(false);

You can call it anywhere in your theme. It returns an array that you can inspect with a:
print_r(wp_get_sidebars_widgets(false));

There is a nested array (the second) that contains the sidebars list and the active widgets for each of them.

You should search for the names of your widgets in this nested array.

2009-12-14

Sébastien Méric answers:

that's right, we need the sidebar id to use is_active_sidebar()...

so this should be working now (!)

copy/paste this in your function.php :

// Register widgets area(s)
if ( function_exists( 'register_sidebar' ) ) {
$sidebars = array(
'Footer Widgets',
'another widget area'
);
foreach ( $sidebars as $sidebar ):
register_sidebar( array(
'name' => $sidebar,
'before_widget' => '<li id="%1$s" class="widget widget-%2$s">',
'after_widget' => '</li>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>'
));
endforeach;
}

/**
* Get a sidebar id by name.
*
* @param string $name Widget name.
* @return string Widget id, if widget sidebar with that name was found. Empty if not found.
*/

function get_sidebar_id_by_name( $name='' ) {
// no name specified !
if ( '' == $name ) return '';

global $wp_registered_sidebars;

// no registered sidebar !
if ( !isset( $wp_registered_sidebars ) ) return '';

foreach ( $wp_registered_sidebars as $sidebar ):
if ( $sidebar['name'] == $name ) return $sidebar['id'];
endforeach;

return '';
}


and this in your footer.php :

<div id="footer">
<?php if ( function_exists( 'dynamic_sidebar' ) && function_exists( 'is_active_sidebar' ) && is_active_sidebar( get_sidebar_id_by_name( 'Footer Widgets' ) ) ) { ?>
<ul id="footer-widgets">
<?php dynamic_sidebar( 'Footer Widgets' ); ?>
</ul><!-- /#footer-widgets -->
<?php } ?>
</div><!-- /#footer -->

2009-12-14

Max answers:

The problem could be solved with very few lines of code.

This is a function that given the name of the sidebar containing the widgets and
given an array containing the widgets names, return TRUE if all the widgets are active
in the sidebar and FALSE otherwise.


function are_widgets_active($sidebar_name, $widgets_names)
{
$widgets_info = wp_get_sidebars_widgets();

$result = array_intersect($widgets_names,
$widgets_info[$sidebar_name]);

if(sizeof($result) == sizeof($widgets_names))
return TRUE;
else
return FALSE;
}


An example of invocation:


$sidebar_name = 'footer-1';

$widgets_names = array("archives-4", "calendar-3");

if(are_widgets_active($sidebar_name, $widgets_names))
{
echo 'The group of widgets is active';
}
else
{
echo 'The group of widgets is not active';
}