WordPress Customizer Sanitizacion – Radio Input

function askiw_radio_input_customizer( $wp_customize ) {

        $wp_customize->add_section( 'askiw_customizer_radio_section',
            array(
                'title' => esc_html__( 'Section Title', 'askiw' ),
                'priority' => 1
            )
        );

        function askiw_sanitize_radio( $input, $setting ){
            $input = sanitize_key($input);
            $choices = $setting->manager->get_control( $setting->id )->choices;
            return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
        }

        $wp_customize->add_setting( 'askiw_customizer_radio', 
            array(
                'sanitize_callback' => 'askiw_sanitize_radio'
            )
        );

        $wp_customize->add_control( 'askiw_customizer_radio', 
            array(
                'label' => esc_html__( 'Radio Input', 'askiw' ),
                'section' => 'askiw_customizer_radio_section',
                'type' => 'radio',
                'choices' => array(
                    '1' => esc_html__('Input 1','askiw'),
                    '2' => esc_html__('Input 2','askiw'),
                    '3' => esc_html__('Input 3','askiw'),               
                    '4' => esc_html__('Input 4','askiw')               
                )
            )
        );      

}
add_action( 'customize_register', 'askiw_radio_input_customizer' );

Leave a Reply