Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$20
How do I insert this conditional inside my widgetized area?
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?
This question has been answered.
Darren Hoyt | 12/12/09 at 6:13pm
Edit
(10) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
12/14/09
12:21pmJustin Tadlock says: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. -

Last edited:
12/12/09
7:02pmSébastien Méric says: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 } ?>
-

Last edited:
12/12/09
7:13pmSébastien Méric says: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 } ?> -

Last edited:
12/13/09
1:19amUtkarsh Kukreti says: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; ?> -

Last edited:
12/14/09
12:55amUtkarsh Kukreti says: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; ?>Previous versions of this answer: 12/14/09 at 12:54am | 12/14/09 at 12:55am
-

Last edited:
12/14/09
12:21pmBen says: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. -

Last edited:
12/14/09
7:15amBill Hunt says: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 */ ?>
-

Last edited:
12/14/09
7:18amMax says: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.Previous versions of this answer: 12/14/09 at 7:16am | 12/14/09 at 7:18am
-

Last edited:
12/14/09
9:48amSébastien Méric says: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 -->
Previous versions of this answer: 12/14/09 at 9:17am | 12/14/09 at 9:41am | 12/14/09 at 9:43am | 12/14/09 at 9:44am | 12/14/09 at 9:45am | 12/14/09 at 9:46am | 12/14/09 at 9:47am | 12/14/09 at 9:48am
-

Last edited:
12/14/09
10:51amMax says: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';
}
This question has expired.
Current status of this question: Completed
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
