Is there a sanitization function in the Customizer?

Is there a sanitization function in the Customizer?

Since we should never trust user input, the Customizer API requires to define a callback function for each setting to validate and sanitize input. Unfortunately I often run into the problem that I don’t know or don’t remember the proper WordPress sanitization function for a particular setting.

How to add a checkbox to the Customizer?

At first you must define the setting, via add_setting, important is the param type with value option. After this control the field via add_control and set the param type to checkbox. Alternative it is possible to use select. If I add a default value via std, then work it, also without this param.

How to define a sanitization callback function in WordPress?

The following code examples below will demonstrate how to define sanitization callback functions for various data types. For order’s sake, the codes also include the method to add a section and a setting in Theme Customizer. function theme_slug_customizer ( $wp_customize ) {

How to sanitize filters in PHP 7.3.0?

Apply addslashes (). (Available as of PHP 7.3.0) Remove all characters except digits, +- and optionally .,eE . Remove all characters except digits, plus and minus sign. HTML-encode ‘”<>& and characters with ASCII value less than 32, optionally strip or encode other special characters.

Which is the default setting for the Customizer?

It is usually most important to set the default value of the setting as well as its sanitization callback, which will ensure that no unsafe data is stored in the database. Typical theme usage: Note that the Customizer can handle options stored as keyed arrays for settings using the option type.

Is there a function to sanitize text field?

The sanitize_text_field () function only works on a string, not an array’d item. I located this nice little tidbit of code to sanitize an array, properly. * they encourage usage of sanitize_text_field (). That only works with a single

Who is the creator of the sanitize function in WordPress?

IMHO, this needs to become a core feature of WordPress’ sanitation functions. Lior Broshi is the gentleman that came up with this creative solution (I have obtained his permission to share this). You must log in before being able to contribute a note or feedback.