0

I'm trying to create a custom author info box on homepage which will automaically generate by getting the info from user profile when a user is selected from theme customizer.

I followed the code for user list dropdown in this article and it worked perfectly for me. but I don't know how to sanitize it.

Github Link for Custom_User_Dropdown_control

The following is the example of code I used.

/* custom user dropdown control */
$wp_customize->add_setting( 'custom_user_id', array(
    'default'           => '',
    'sanitize_callback'     => 'custom_sanitize_user_id',
));
$wp_customize->add_control( new Custom_User_Dropdown_Control( $wp_customize, 'custom_user_id', array(
    'label'             => __( 'Select Author', 'text domain' ),
    'section'           => 'custom_author_info',
    'settings'          => 'custom_user_id',
)));

I Just want to sanitize the custom user list dropdown control.

I know how to sanitize a normal select list, but for this I have no Idea how to do it. The following is the example of code which I use for sanitizing normal select list.

function example_sanitize_code( $input ) {
    $valid = array(
        'option1' => 'option1',
        'option2' => 'option2',
        'option3' => 'option3',
    );

    if ( array_key_exists( $input, $valid ) ) {
       return $input;
    } else {
       return '';
    }
}

Can anyone help? :)

  • Support for third party plugins is off-topic here. But, you can also use esc_attr or any of the other escaping functions (for data coming out of the db) or sanitization functions (for data going in) baked into WordPress core so you don't have to go roll your own. – Andrew Bartel Dec 4 '14 at 17:48
2

If you want to validate the data then you'll have to try retrieve user via ID.

To only sanitize ID since it's numeric is enough to cast it as integer, absint() will work fine as a callback.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.