Skip to content

Instantly share code, notes, and snippets.

@ajskelton
Last active June 24, 2021 19:36
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save ajskelton/8ae331406bd99254874b42c69ff0aa48 to your computer and use it in GitHub Desktop.
Save ajskelton/8ae331406bd99254874b42c69ff0aa48 to your computer and use it in GitHub Desktop.
Add a Select field to the WordPress Customizer.
$wp_customize->add_setting( 'themeslug_select_setting_id', array(
'capability' => 'edit_theme_options',
'sanitize_callback' => 'themeslug_sanitize_select',
'default' => 'value1',
) );
$wp_customize->add_control( 'themeslug_select_setting_id', array(
'type' => 'select',
'section' => 'custom_section', // Add a default or your own section
'label' => __( 'Custom Select Option' ),
'description' => __( 'This is a custom select option.' ),
'choices' => array(
'value1' => __( 'Value 1' ),
'value2' => __( 'Value 2' ),
'value3' => __( 'Value 3' ),
),
) );
function themeslug_sanitize_select( $input, $setting ) {
// Ensure input is a slug.
$input = sanitize_key( $input );
// Get list of choices from the control associated with the setting.
$choices = $setting->manager->get_control( $setting->id )->choices;
// If the input is a valid key, return it; otherwise, return the default.
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
@safiro
Copy link

safiro commented Jul 3, 2017

How to display the result in header.php

@alliedc
Copy link

alliedc commented Jul 5, 2017

thinking the same.

@JacobDB
Copy link

JacobDB commented Jul 7, 2017

@safiro, @alliedc, to retrieve the value, use:

<?php echo get_theme_mod("themeslug_select_setting_id"); ?>

@safiro
Copy link

safiro commented Jul 8, 2017

@JacobDB It Works, Thanks.

@Yeadh
Copy link

Yeadh commented Sep 23, 2017

I can't display the result.
I want to change the Background color with this select Box.
Please Help

@aumkarthakur
Copy link

Does this code even work anymore ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment