Awwwards
TopDesignKing

WordPress Customizer sanitization

The Customizer API demands a callback function be defined for each setting to validate and sanitize input because we should never trust user input. Unfortunately, I frequently have the issue of not knowing or remembering the appropriate WordPress sanitization function for a specific setup.

The definition of sanitization callback functions for different data types is shown in the following code snippets. The codes also contain how to create a section and a setting in Theme Customizer for the purpose of order.

/**
 * SANITIZE RADIO BOX
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_radio( $wp_customize ) {

    $wp_customize->add_section(
        'weszty_web_customizer_radio_section',
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );

    // radio box sanitization function
    function weszty_web_sanitize_radio( $input, $setting ) {

        // input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
        $input = sanitize_key( $input );

        // get the list of possible radio box options 
        $choices = $setting->manager->get_control( $setting->id )->choices;

        // return input if valid or return default option
        return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
    }

    // add setting to your section
    $wp_customize->add_setting(
        'weszty_web_customizer_radio',
        array(
            'sanitize_callback' => 'weszty_web_sanitize_radio'
        )
    );

    $wp_customize->add_control(
        'weszty_web_customizer_radio',
        array(
            'label' => esc_html__( 'Your Setting with Radio Box', 'weszty_web' ),
            'section' => 'weszty_web_customizer_radio_section',
            'type' => 'radio',
            'choices' => array(
                'one' => esc_html__( 'Choice One', 'weszty_web' ),
                'two' => esc_html__( 'Choice Two', 'weszty_web' ),
                'three' => esc_html__( 'Choice Three', 'weszty_web' )
            )
        )
    );
}
add_action( 'customize_register', 'weszty_web_customizer_radio' );
/**
 * SANITIZE CHECKBOX
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_checkbox( $wp_customize ) {

    $wp_customize->add_section(
        'weszty_web_customizer_checkbox_section',
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );

    // checkbox sanitization function
    function weszty_web_sanitize_checkbox( $input ) {

        // returns true if checkbox is checked
        return ( isset( $input ) ? true : false );
    }

    // add setting to your section
    $wp_customize->add_setting(
        'weszty_web_customizer_checkbox_checkbox',
        array(
            'default' => '',
            'sanitize_callback' => 'weszty_web_sanitize_checkbox'
        )
    );

    $wp_customize->add_control(
        'weszty_web_customizer_checkbox_checkbox',
        array(
            'label' => esc_html__( 'Your Setting with Checkbox', 'weszty_web' ),
            'section' => 'weszty_web_customizer_checkbox_section',
            'type' => 'checkbox'
        )
    );
}
add_action( 'customize_register', 'weszty_web_customizer_checkbox' );
/**
 * SANITIZE SELECT OPTIONS
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_select( $wp_customize ) {           
  
    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );
    
    // select sanitization function
    function weszty_web_sanitize_select( $input, $setting ) {
        
        // input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
        $input = sanitize_key( $input );

        // get the list of possible select options
        $choices = $setting->manager->get_control( $setting->id )->choices;
                            
        // return input if valid or return default option
        return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                
            
    }
    
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_select', 
        array(
            'sanitize_callback' => 'weszty_web_sanitize_select'
        )
    );
        
    $wp_customize->add_control( 
        'weszty_web_customizer_select', 
        array(
            'label' => esc_html__( 'Your Setting with select', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'select',
            'choices' => array(
                '' => esc_html__( 'Please select','weszty_web' ),
                'one' => esc_html__( 'Choice One','weszty_web' ),
                'two' => esc_html__( 'Choice Two','weszty_web' ),
                'three' => esc_html__( 'Choice Three','weszty_web' )
            )
        )
    );      
      
}
add_action( 'customize_register', 'weszty_web_customizer_select' );

It is sufficient to use wp_filter_nohtml_kses() directly in the callback to allow simple text only.

/**
 * SANITIZE TEXT AND TEXTAREA INPUT
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_input( $wp_customize ) {           
  
    $wp_customize->add_section( 
        'weszty_web_customizer_input_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );
             
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_input_text', 
        array(
            'sanitize_callback' => 'wp_filter_nohtml_kses' // removes all HTML from content
        )
    );
        
    $wp_customize->add_control( 
        'weszty_web_customizer_input_text', 
        array(
            'label' => esc_html__( 'Your Setting with text input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_input_section',
            'type' => 'text'
        )
    );
      
}
add_action( 'customize_register', 'weszty_web_customizer_input' );
/**
 * SANITIZE EMAIL
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_email( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
          
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_email', 
        array(
            'sanitize_callback' => 'sanitize_email' // removes all invalid characters
        )
    );
    
    $wp_customize->add_control( 
        'weszty_web_customizer_email', 
        array(
            'label' => esc_html__( 'Your Setting with email input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'email'
        )
    );      
    
}
add_action( 'customize_register', 'weszty_web_customizer_email' );
/**
 * SANITIZE URL
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_url( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
          
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_url', 
        array(
            'sanitize_callback' => 'esc_url_raw' // cleans URL from all invalid char
        )
    );
    
    $wp_customize->add_control( 
        'weszty_web_customizer_url', 
        array(
            'label' => esc_html__( 'Your Setting with URL input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'url'
        )
    );      
    
}
add_action( 'customize_register', 'weszty_web_customizer_url' );
/**
 * SANITIZE NUMBER
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_number( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
          
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_number', 
        array(
            'sanitize_callback' => 'absint' // converts value to a non-negative integer
        )
    );
    
    $wp_customize->add_control( 
        'weszty_web_customizer_number', 
        array(
            'label' => esc_html__( 'Your Setting with number input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'number'
        )
    );      
    
}
add_action( 'customize_register', 'weszty_web_customizer_number' );
/**
 * SANITIZE DROP-DOWN PAGES
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_drop_down( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
          
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_drop_down', 
        array(
            'sanitize_callback' => 'absint' // converts value to a non-negative integer
        )
    );
    
    $wp_customize->add_control( 
        'weszty_web_customizer_drop_down', 
        array(
            'label' => esc_html__( 'Your Setting with dropdown_pages input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'dropdown-pages'
        )
    );      
    
}
add_action( 'customize_register', 'weszty_web_customizer_drop_down' );
/**
 * SANITIZE FILE INPUT
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_file( $wp_customize ) {           
  
    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
    
    // file input sanitization function
    function weszty_web_sanitize_file( $file, $setting ) {
        
        // allowed file types
        $mimes = array(
            'jpg|jpeg|jpe' => 'image/jpeg',
            'gif'          => 'image/gif',
            'png'          => 'image/png'
        );
            
        // check file type from file name
        $file_ext = wp_check_filetype( $file, $mimes );
            
        // if file has a valid mime type return it, otherwise return default
        return ( $file_ext['ext'] ? $file : $setting->default );
    }

    // add select setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_file', 
        array(
            'sanitize_callback' => 'weszty_web_sanitize_file'
        )
    );

    $wp_customize->add_control( 
        new WP_Customize_Upload_Control( 
            $wp_customize, 
            'weszty_web_customizer_file', 
            array(
                'label'      => esc_html__( 'Your Setting with file input', 'weszty_web' ),
                'section'    => 'weszty_web_customizer_section'                   
            )
        ) 
    );              
      
}
add_action( 'customize_register', 'weszty_web_customizer_file' );
/**
 * SANITIZE CSS
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_css( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );
           
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_css', 
        array(
            'sanitize_callback' => 'wp_strip_all_tags' // strip all HTML tags including script and style
        )
    );
        
    $wp_customize->add_control( 
        'weszty_web_customizer_css', 
        array(
            'label' => esc_html__( 'Your Setting with CSS input', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'textarea'
        )
    );
       
}
add_action( 'customize_register', 'weszty_web_customizer_css' );
/**
 * SANITIZE CSS HEX COLOR CODE
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_hex( $wp_customize ) {           
  
    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );
                   
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_color', 
        array(
            'default' => '#000000',
            'sanitize_callback' => 'sanitize_hex_color' // validates 3 or 6 digit HTML hex color code
        )
    );
    
    $wp_customize->add_control( 
        new WP_Customize_Color_Control( 
        $wp_customize, 
        'weszty_web_customizer_color', 
            array(              
                'label'      => esc_html__( 'Your Setting with color input', 'weszty_web' ),
                'section'    => 'weszty_web_customizer_section'       
            )
        ) 
    );
       
}
add_action( 'customize_register', 'weszty_web_customizer_hex' );

Use the wp_kses() method, which only saves HTML tags that are permitted in post content.

/**
 * SANITIZE HTML
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_html( $wp_customize ) {           
  
    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );
        
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_html_code', 
        array(
            'sanitize_callback' => 'wp_kses_post' // keeps only HTML tags that are allowed in post content
        )
    );
        
    $wp_customize->add_control( 
        'weszty_web_customizer_html_code', 
        array(
            'label' => esc_html__( 'Your Setting with HTML code', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'textarea'
        )
    );         
       
}
add_action( 'customize_register', 'weszty_web_customizer_html' );

As an alternative, you can define the permitted HTML tags and attributes using the wp_kses() method as follows:

$allowed_html = array(
    'a' => array(
        'href' => array(),
        'title' => array()
    ),
    'br' => array(),
    'em' => array(),
    'strong' => array(),
);
  
wp_kses( $input, $allowed_html );

I will be using base64_encode() function to save the script in database properly and base64_decode() function to escape the script for the textarea in Customizer. Also use base64_decode() function on frontend to echo the script.

/**
 * SANITIZE JAVASCRIPT
 *
 * @param [type] $wp_customize
 * @return void
 */
function weszty_web_customizer_js( $wp_customize ) {           

    $wp_customize->add_section( 
        'weszty_web_customizer_section', 
        array(
            'title' => esc_html__( 'Your Section', 'weszty_web' ),
            'priority' => 150
        )
    );      
    
    // script input sanitization function
    function weszty_web_sanitize_js_code( $input ) {
        return base64_encode( $input );
    }
    
    // output escape function    
    function weszty_web_escape_js_output( $input ) {
        return esc_textarea( base64_decode( $input ) );
    }
          
    // add setting to your section
    $wp_customize->add_setting( 
        'weszty_web_customizer_js_code', 
        array(          
            'sanitize_callback' => 'weszty_web_sanitize_js_code', // encode for DB insert
            'sanitize_js_callback' => 'weszty_web_escape_js_output' // escape script for the textarea
        )
    );
        
    $wp_customize->add_control( 
        'weszty_web_customizer_js_code', 
        array(
            'label' => esc_html__( 'Your Setting with JS code', 'weszty_web' ),
            'section' => 'weszty_web_customizer_section',
            'type' => 'textarea'
        )
    );         
    
}
add_action( 'customize_register', 'weszty_web_customizer_js' );

LIST OF OTHER WORDPRESS SANITIZATION FUNCTIONS

Don't be weird.

Would you like more information or do you have a question?

scroll
10%
Drag View Close play