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 have a custom widget that I want to apply conditional logic to, depending on its parent sidebar's registered ID.

For example, my theme has 6 registered sidebar widget areas.

I have a custom widget that may be loaded into any of these 6 sidebar widget areas.

Is there a way that I can script the widget code below in order to determine if the widget is loaded into one or more of the registered sidebar widget areas?

For example, I'm registering my sidebar widget areas like so:

register_sidebar(array(
    'name' => 'Footer Area One',
    'id' => 'home-footer-widget',
    'description' => __( 'Appears just above the main footer area. Spans across the entire width of page'),
    'before_widget' => '<div class="footer-pre-home %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4>',
    'after_title' => '</h4>',
));

register_sidebar(array(
    'name' => 'Footer Area Two',
    'id' => 'footer-two',
    'description' => __( 'Appears just above the main footer area. Spans across the entire width of page'),
    'before_widget' => '<div class="footer-two %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h4>',
    'after_title' => '</h4>',
));

And I have a custom widget which has this code:

function widget( $args, $instance ) {
    extract($args);
    $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
    $text = apply_filters( 'widget_text', $instance['text'], $instance );
    $hide_title = isset( $instance['hide_title'] ) ? $instance['hide_title'] : false;
    $filter = isset( $instance['filter'] ) ? $instance['filter'] : false;
    echo $before_widget;
    if ( $title && !$hide_title )echo $before_title . $title . $after_title;
    if ( $text ) echo $filter ? wpautop($text) : $text;
    echo $after_widget;
}

And I want to place code into the function widget() that determines if the widget is in either of the two named registered sidebar widgets above. If it does happen to be in one of those two named sidebars, I will not echo the before/after widget code in that case.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The information you want is in the $args parameter.

var_dump($args); // inside your widget method

Look at the 'name' and 'id' array elements.

Your code should look something like:

function widget( $args, $instance ) {
    extract($args);
    $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
    $text = apply_filters( 'widget_text', $instance['text'], $instance );
    $hide_title = isset( $instance['hide_title'] ) ? $instance['hide_title'] : false;
    $filter = isset( $instance['filter'] ) ? $instance['filter'] : false;
    if ('home-footer-widget' != $args['id'] && 'footer-two' != $args['id']) echo $before_widget;
    if ( $title && !$hide_title )echo $before_title . $title . $after_title;
    if ( $text ) echo $filter ? wpautop($text) : $text;
    if ('home-footer-widget' != $args['id'] && 'footer-two' != $args['id']) echo $after_widget;
}
share|improve this answer
    
+1 Thanks! Works a charm. –  N2Mystic Feb 3 '13 at 0:13

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.