apply_filters( “sanitize_option_{$option}”, string $value, string $option, string $original_value )

Filters an option value following sanitization.

Parameters

$valuestring
The sanitized option value.
$optionstring
The option name.
$original_valuestring
The original value passed to the function.

More Information

There is one filter per option name; the $option in the filter name stands for the name (e.g. ‘sanitize_option_blogname‘, ‘sanitize_option_siteurl‘). You can use this filter to define a sanitizer for your own options. See the notes for sanitize_option() for a list of existing options.

Filter existing options

add_filter('sanitize_option_admin_email', 'sanitize_builtin_option', 10, 2);
add_filter('sanitize_option_new_admin_email', 'sanitize_builtin_option', 10, 2);

function sanitize_builtin_option($value, $option) {
//...
}

Filter your own options

add_filter('sanitize_option_feed_url', 'sanitize_url', 10, 2);
add_filter('sanitize_option_wpi_endpoint', 'sanitize_url', 10, 2);
add_filter('sanitize_option_contact_email', 'sanitize_email');

function sanitize_url($value, $option) {
//...
}

function sanitize_email($value, $option) {
//...
}

Source

return apply_filters( "sanitize_option_{$option}", $value, $option, $original_value );

Changelog

VersionDescription
4.3.0Added the $original_value parameter.
2.3.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Example Migrated from Codex:

    add_option('blocksize', 200); # 256 gets stored for blocksize
    
    add_filter('sanitize_option_blocksize', 'sanitize_blocksize');
    
    if (! function_exists('lg')) {
        function lg($x) {
            return log($x) / log(2);
        }
    }
    
    function sanitize_blocksize($value) {
        return pow(2, ceil(lg($value)));
    }
  2. Skip to note 4 content

    To be more clear, $original_value holds the value before any sanitize_option_{$option} filter has been applied to the current option.

    So if $value is originally 'My Value' and a filter with higher priority has sanitized it into 'my_value', what we will receive is:

    $value = 'my_value'
    $option = 'the_option_name'
    $original_value = 'My Value'

    $original_value is NOT the original option value, but the new option value before sanitization.

You must log in before being able to contribute a note or feedback.