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.

In the script below, which resides in a custom widget, I'm trying to determine which sidebar area the widget has been added to because I want to alter the default text depending on which sidebar the user has added the widget to. This code should only execute on the admin side while editing the widget.

However, regardless of which sidebar the widget is placed in (my theme has 4 - main sidebar, header, footer and below content), it echoes the "match" string in all of them.

In this case, I only want the echo to fire, when the widget is inside the widgetized sidebar whose id is "home-header-widget".

What's wrong with the if/then?

function form( $instance ) {

    global $wp_registered_widgets, $wp_registered_sidebars;
    $sidebars_widgets = get_option('sidebars_widgets');
    if($sidebars_widgets["home-header-widget"]) echo 'Match';

    $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'hide_title' => '' ) );
    $title = format_to_edit($instance['title']);
    $text = format_to_edit($instance['text']);
        if($text==''){
        $text='hello world';
    }
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted
    $sideBars = wp_get_sidebars_widgets ();
    $mySideBar = false;
    foreach ($sideBars as $sideBarName => $sideBar) {
      if (array_search ($this->id, $sideBar) !== false) {
        $mySideBar = $sideBarName;
        break;
      }
    }
share|improve this answer
    
Works a charm! Didn't think it was possible. –  N2Mystic Dec 5 '12 at 22:38
add comment
$widgets = wp_get_sidebars_widgets();
if( in_array( $this->id, $widgets['home-header-widget'] ) ) { 
    echo 'Match';
}

This should work, but only if the user has already saved the widget and not when they first dragged it to the home-header-widget area.

share|improve this answer
add comment

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.