How to determine which registered sidebar area a custom widget is loaded into

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  1. 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;
    }