1

I am using wordpress 4.7.2 .

I am creating a customization section for my custom theme. This is my select control.

 $wp_customize->add_setting( $this->slug . '_blog[pagination_type]', array(
            'default'           => 'classic-pagination',
            'transport'         => 'postMessage',
            'type'              => 'theme_mod',
            'sanitize_callback' => 'my_theme_sanitize_select',
        ) );

        $wp_customize->add_control( $this->slug . '_blog[pagination_type]', array(
            'label'    => 'Pagination Type',
            'section'  => $this->slug . '_blog_section',
            'type'     => 'select',
            'choices'  => array(
                'classic-pagination' => 'Classic Pagination',
                'load-more-button'   => 'Load More Button'
                ),
            'priority' => 18,
        ) );

and my sanitize_callback function is

public function my_theme_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 );
}

Displaying error Invalid value , when trying to save data.

I tried even with just sanitize_key function but can not get it correct

public function my_theme_sanitize_select( $input ) {
        return sanitize_key( $input );
    }

and then even to

public function my_theme_sanitize_select( $input ) {
        return $input;
    }

and

public function my_theme_sanitize_select( $input ) {
        return true;
    }

but when I am not calling to any sanitization function it is working correctly . that means I am making some mistake in creating the sanitize_callback() but in reference too the callback function has same naming and same function definition.

after that I searched for similar problem and got one but could not solve this issue, please help me.

I gone through the core codes and found in WP_Customize_Setting class sanitize() my input data is becoming null . but could not get more idea.

  • where are you putting all this? is a class? or in functions.php? – David Lee Feb 20 '17 at 15:19
  • @DavidLee A class . registering through constructor of parent class. – Prafulla Kumar Sahu Feb 20 '17 at 15:21
1

The add_setting logic cant find the my_theme_sanitize_select function, you need to tell it where it is:

'sanitize_callback'    => array( $this, 'my_theme_sanitize_select' ),

remember that your function is inside a class so it cant just be accessed, add this to your class:

public function my_theme_sanitize_select( $input, $setting ) {
    $from_parent = my_theme_sanitize_select( $input, $setting );
    return $from_parent;
}
  • sanitization function is in parent class of this class, so it should is able to access it, the issue might be something else. – Prafulla Kumar Sahu Feb 20 '17 at 15:43
  • let me update the answer, please give it a try. – David Lee Feb 20 '17 at 15:48
  • thank you for your effort buddy, but sadly No success with it. :( Can you please suggest any other possible option ? – Prafulla Kumar Sahu Feb 20 '17 at 15:59
  • i updated my answer, you are getting an invalid value data null because the function scope – David Lee Feb 20 '17 at 16:07
  • This also giving same error. I create this method in the same class I am creating control , but got same error . :( – Prafulla Kumar Sahu Feb 20 '17 at 16:24

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.