Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielyewright/910b1a7fc4122d1f82354c0482112f55 to your computer and use it in GitHub Desktop.
Save danielyewright/910b1a7fc4122d1f82354c0482112f55 to your computer and use it in GitHub Desktop.
Add a Radio field to the WordPress Customizer.
$wp_customize->add_setting( 'themeslug_radio_setting_id', array(
'capability' => 'edit_theme_options',
'default' => 'blue',
'sanitize_callback' => 'themeslug_customizer_sanitize_radio',
) );
$wp_customize->add_control( 'themeslug_radio_setting_id', array(
'type' => 'radio',
'section' => 'custom_section', // Add a default or your own section
'label' => __( 'Custom Radio Selection' ),
'description' => __( 'This is a custom radio input.' ),
'choices' => array(
'red' => __( 'Red' ),
'blue' => __( 'Blue' ),
'green' => __( 'Green' ),
),
) );
function themeslug_customizer_sanitize_radio( $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