0

I've got the following functions as in this pastebin in a child theme:

http://pastebin.com/uzjmXiyN

This functions creates a drop down box from where you can choose a sidebar for use on a specific page. Everything works fine after a lot of sweat. My only problem is the

$wp_registered_sidebars

function in the following piece of code

foreach($wp_registered_sidebars as $sidebar_id => $sidebar)
{
    $output .= "<option";
    if($sidebar_id == $val)
        $output .= " selected='selected'";
    $output .= " value='".$sidebar_id."'>".$sidebar['name']."</option>";
}

$output .= "</select>";

echo $output;

This function lists all the available sidebars of the theme, and this is what I don't want. I only want the drop down box to display the sidebars in the

$dynamic_widget_areas

array. If I replace

$wp_registered_sidebars

with

$dynamic_widget_areas

I get the following errors:

Notice: Undefined variable: dynamic_widget_area ...etc

and

Warning: Invalid argument supplied for foreach() ...etc

What is the correct way to display only certain sidebars.

1 Answer 1

1

This is a generic PHP question. You need to check whether the $sidebar_id key is set in $dynamic_widget_areas as you loop through. This proof of concept code should give you the idea.

$dynamic_widget_areas = array(
  'sidebar-1' => 'stuff',
);
global $wp_registered_sidebars;
$output = '';
foreach($wp_registered_sidebars as $sidebar_id => $sidebar) {
  $output .= "<option";
  if(isset($dynamic_widget_areas[$sidebar_id])) {
     if($sidebar_id == $val) {
      $output .= " selected='selected'";
    }
    $output .= " value='".$sidebar_id."'>".$sidebar['name']."</option>";
  }
}
$output .= "</select>";

Not the answer you're looking for? Browse other questions tagged or ask your own question.