Last active
July 22, 2021 03:41
-
-
Save svebal/9d792e6805cf39c068549650bfd9cff1 to your computer and use it in GitHub Desktop.
WP Customizer Control for Select field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$wp_customize->add_setting( 'themeslug_select_setting_id', array( | |
'capability' => 'edit_theme_options', | |
'sanitize_callback' => 'themeslug_sanitize_select', | |
'default' => 'option1', | |
) ); | |
$wp_customize->add_control( 'themeslug_select_setting_id', array( | |
'type' => 'select', | |
'section' => 'custom_section', // Add a default or your own section | |
'label' => __( 'Select Option' ), | |
'description' => __( 'Description for select option.' ), | |
'choices' => array( | |
'option1' => __( 'Option 1' ), | |
'option1' => __( 'Option 2' ), | |
'option1' => __( 'Option 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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment