Using WordPress ‘customize_sanitize_js_{$this->id}’ PHP filter

The customize_sanitize_js_{$this->id} WordPress PHP filter sanitizes a Customize setting value for use in JavaScript.

Usage

add_filter('customize_sanitize_js_{$this->id}', 'your_custom_function', 10, 2);

function your_custom_function($value, $setting) {
    // Your custom code here

    return $value;
}

Parameters

  • $value (mixed): The setting value to be sanitized.
  • $setting (WP_Customize_Setting): WP_Customize_Setting instance representing the setting.

More information

See WordPress Developer Resources: customize_sanitize_js_{$this->id}

Examples

Sanitize integer value for JavaScript

Sanitize an integer value before using it in JavaScript.

add_filter('customize_sanitize_js_example_integer', 'sanitize_integer_value', 10, 2);

function sanitize_integer_value($value, $setting) {
    // Ensure the value is an integer
    return intval($value);
}

Sanitize boolean value for JavaScript

Sanitize a boolean value before using it in JavaScript.

add_filter('customize_sanitize_js_example_boolean', 'sanitize_boolean_value', 10, 2);

function sanitize_boolean_value($value, $setting) {
    // Ensure the value is a boolean
    return boolval($value);
}

Sanitize email value for JavaScript

Sanitize an email value before using it in JavaScript.

add_filter('customize_sanitize_js_example_email', 'sanitize_email_value', 10, 2);

function sanitize_email_value($value, $setting) {
    // Sanitize the email address
    return sanitize_email($value);
}

Sanitize URL value for JavaScript

Sanitize a URL value before using it in JavaScript.

add_filter('customize_sanitize_js_example_url', 'sanitize_url_value', 10, 2);

function sanitize_url_value($value, $setting) {
    // Sanitize the URL
    return esc_url_raw($value);
}

Sanitize text value for JavaScript

Sanitize a text value before using it in JavaScript.

add_filter('customize_sanitize_js_example_text', 'sanitize_text_value', 10, 2);

function sanitize_text_value($value, $setting) {
    // Sanitize the text
    return sanitize_text_field($value);
}