0

I need to build a select on my theme customise screen from this foreach:

foreach($wp_registered_sidebars as $sidebar_id => $sidebar)
{
    if($sidebar_id == $val)

    $sidebar_id.$sidebar['name']
}

Here is the code for the setting and control for custom sidebar selection.

The dynamic list would need to go in the 'choices' => array();

Is this even possible?

    // Add Layout setting
$wp_customize->add_setting(
    // ID
    'sidebar_left_selection',
    // Arguments array
    array(
        'default' => 'none',
        'type' => 'option',
    )
);

// Add Layout control
$wp_customize->add_control(
    // ID
    'sidebar_left_selection',
    // Arguments array
    array(
        'type' => 'select',
        'label' => __( 'Sidebar Left Selection', 'webcodexcustomizer' ),
        'section' => 'layout_section',
        'choices' => array(
            'none' => __( 'None', 'webcodexcustomizer' ),
            'sidebar_test' => __( 'Test Sidebar', 'webcodexcustomizer' ),
        ),
        'priority' => 36
    )
);

1 Answer 1

1

Figured it out, you have to create a new class extending WP_Customize_Control:

    // Add Layout setting
$wp_customize->add_setting(
    // ID
    'sidebar_left_selection',
    // Arguments array
    array(
        'default' => 'none',
        'sanitize_callback' => 'webcodexcustomizer_sanitize_sidebar_selection'
    )
);

 class customize_sidebar_selection extends WP_Customize_Control {
 public function render_content() 
 {
global $wp_registered_sidebars;


// The actual fields for data entry
$output = "<span>" . esc_html( $this->label ) . "</span>";
$output .= "<select name='custom_sidebar_left'>";

// Add a default option
$output .= "<option";
if($val == "default")
    $output .= " selected='selected'";
$output .= " value='default'>".__('default', $themename)."</option>";

// Fill the select element with all registered sidebars
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;
}
}

// Add Layout control
$wp_customize->add_control( 
    new customize_sidebar_selection( 
        $wp_customize, 'sidebar_left_selection', array(
            'label' => 'Sidebar Left Selection',
            'section' => 'layout_section',
            'priority' => 36,
        ) 
    ) 
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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