• Resolved limbenjamin

    (@limbenjamin)


    I am using the WP customizer to allow users to customise the html class of a certain element. As required, I have added the sanitize callback to sanitize user input. I would like the user to be able to set it to either a valid html class or an empty string. However, if an empty string is entered, wordpress will fail to save the customization. How can I set the $fallback value in such a case?

    $wp_customize->add_setting( ‘voce_social_icon_1’ , array(‘default’ => ‘fa-facebook’, ‘sanitize_callback’ => ‘sanitize_html_class’,));

    I am only having such problems with ‘sanitize_html_class’, ‘esc_url_raw’ happily accepts an empty string.

    • This topic was modified 7 years, 9 months ago by limbenjamin.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Because of the way the setting save method is constructed, it’s impossible save an empty string that way. Perhaps you could save a special value when an empty string is desired, say “!empty!”. Make your own sanitize callback that uses sanitize_html_class() when a value is not empty. In the case of empty values, return “!empty!”. Then in whatever function utilizes this value for output, when “!empty!” is encountered, replace it with an empty string.

    Another possible workaround is to again use your own sanitize callback that uses sanitize_html_class(). When an empty string is passed, your callback could explicitly save the value in the DB by some other method than the setting object’s save method. Then go ahead and return the empty string, which will cause the object’s save method to abort, leaving what you explicitly saved intact.

    To save such a value, you’ll need to first retrieve the entire array of values, alter this one thing, then save the entire array back.

    Thread Starter limbenjamin

    (@limbenjamin)

    Thanks. Have updated my theme with my own callback function that uses sanitize_html_class().

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sanitize Callback fallback’ is closed to new replies.