wppaste
WordPress

sanitize_option( string $option, mixed $value ): mixed

Since
2.0.5
Source
wp-includes/formatting.php:4826
Sanitizes various option values based on the nature of the option.

Description

This is basically a switch statement which will pass $value through a number of functions depending on the $option.

Compatibility

WordPress
since 2.0.5
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$optionstring
The name of the option.
$valuemixed
The unsanitized value.

Return value

mixed
Sanitized value.

Performance profile

How much work a call to sanitize_option() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
20–77

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 584.

Plugin surface
1 hook

Third-party callbacks on 'sanitize_option_{$option}' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below sanitize_option()
  • serializeserialisationmaybe_unserialize()one call below sanitize_option()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 87 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_option() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$error === null20–23apply_filters()
$error === null21absint(), apply_filters()
$value && $error === null26–27abs(), apply_filters()
$error !== null && $error !== "" && !function_exists()30–33get_option(), function_exists(), apply_filters()
$error !== null && $error !== "" && !function_exists()31absint(), get_option(), function_exists(), apply_filters()
is_wp_error() && $error === null32–34->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && !function_exists()34–37is_wp_error(), get_option(), function_exists(), apply_filters()
$value === false && !is_wp_error() && $error === null35->strip_invalid_text_for_column(), is_wp_error(), esc_html(), apply_filters()
!is_wp_error() && !$value && $error === null35->strip_invalid_text_for_column(), is_wp_error(), __(), apply_filters()
!is_wp_error() && $value && $error === null35->strip_invalid_text_for_column(), is_wp_error(), sanitize_url(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && !function_exists()35absint(), is_wp_error(), get_option(), function_exists(), apply_filters()
$error !== null && $error !== "" && function_exists()36–39get_option(), function_exists(), add_settings_error(), apply_filters()
75 further outcomes, up to 77 instructions
$value && $error !== null && $error !== "" && !function_exists()36–37abs(), get_option(), function_exists(), apply_filters()
!is_wp_error() && is_email() && $error === null36->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), apply_filters()
$error !== null && $error !== "" && function_exists()37absint(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && $error === null37->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && function_exists()40–43is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value && $error !== null && $error === "" && !is_wp_error() && !function_exists()40–41abs(), is_wp_error(), get_option(), function_exists(), apply_filters()
$error === null40explode(), array_map(), array_filter(), array_map(), array_filter(), apply_filters()
!is_wp_error() && !is_email() && $error === null41->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && function_exists()41absint(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value && $error !== null && $error !== "" && function_exists()42–43abs(), get_option(), function_exists(), add_settings_error(), apply_filters()
is_wp_error() && $error !== null && $error !== "" && !function_exists()42–44->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), get_option(), function_exists(), apply_filters()
$value === false && !is_wp_error() && $error !== null && $error !== "" && !function_exists()45->strip_invalid_text_for_column(), is_wp_error(), esc_html(), get_option(), function_exists(), apply_filters()
$value !== false && is_wp_error() && $error === null45->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), apply_filters()
!is_wp_error() && !$value && $error !== null && $error !== "" && !function_exists()45->strip_invalid_text_for_column(), is_wp_error(), __(), get_option(), function_exists(), apply_filters()
!is_wp_error() && $value && $error !== null && $error !== "" && !function_exists()45->strip_invalid_text_for_column(), is_wp_error(), sanitize_url(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && is_wp_error() && !function_exists()45–48is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value && $error !== null && $error === "" && !is_wp_error() && function_exists()46–47abs(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && !is_wp_error() && $error === null46->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), apply_filters()
!is_wp_error() && is_email() && $error !== null && $error !== "" && !function_exists()46->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && !function_exists()46–48->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && is_wp_error() && !function_exists()46absint(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
!is_wp_error() && $error !== null && $error !== "" && !function_exists()47->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), get_option(), function_exists(), apply_filters()
is_wp_error() && $error !== null && $error !== "" && function_exists()48–50->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value === false && !is_wp_error() && $error !== null && $error === "" && !function_exists()49->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), get_option(), function_exists(), apply_filters()
!is_wp_error() && !$value && $error !== null && $error === "" && !function_exists()49->strip_invalid_text_for_column(), is_wp_error(), __(), is_wp_error(), get_option(), function_exists(), apply_filters()
!is_wp_error() && is_email() && $error !== null && $error === "" && !function_exists()50->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), is_wp_error(), get_option(), function_exists(), apply_filters()
$error !== null && $error !== "" && !function_exists()50explode(), array_map(), array_filter(), array_map(), array_filter(), get_option(), function_exists(), apply_filters()
$value === false && !is_wp_error() && $error !== null && $error !== "" && function_exists()51->strip_invalid_text_for_column(), is_wp_error(), esc_html(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && !$value && $error !== null && $error !== "" && function_exists()51->strip_invalid_text_for_column(), is_wp_error(), __(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && $value && $error !== null && $error !== "" && function_exists()51->strip_invalid_text_for_column(), is_wp_error(), sanitize_url(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && !is_email() && $error !== null && $error !== "" && !function_exists()51->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && is_wp_error() && function_exists()51–54is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value && $error !== null && $error === "" && is_wp_error() && !function_exists()51–52abs(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
!is_wp_error() && $error !== null && $error === "" && !function_exists()51->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), is_wp_error(), get_option(), function_exists(), apply_filters()
!is_wp_error() && is_email() && $error !== null && $error !== "" && function_exists()52->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && function_exists()52–54->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && is_wp_error() && function_exists()52absint(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && $error !== null && $error !== "" && function_exists()53->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && !function_exists()54explode(), array_map(), array_filter(), array_map(), array_filter(), is_wp_error(), get_option(), function_exists(), apply_filters()
$value === false && !is_wp_error() && $error !== null && $error === "" && function_exists()55->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && is_wp_error() && $error !== null && $error !== "" && !function_exists()55->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), get_option(), function_exists(), apply_filters()
!is_wp_error() && !$value && $error !== null && $error === "" && function_exists()55->strip_invalid_text_for_column(), is_wp_error(), __(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && !is_email() && $error !== null && $error === "" && !function_exists()55->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), is_wp_error(), get_option(), function_exists(), apply_filters()
$value !== false && !is_wp_error() && $error !== null && $error !== "" && !function_exists()56->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), get_option(), function_exists(), apply_filters()
!is_wp_error() && is_email() && $error !== null && $error === "" && function_exists()56->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error !== "" && function_exists()56explode(), array_map(), array_filter(), array_map(), array_filter(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && !is_email() && $error !== null && $error !== "" && function_exists()57->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value && $error !== null && $error === "" && is_wp_error() && function_exists()57–58abs(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && $error !== null && $error === "" && function_exists()57->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
is_wp_error() && $error !== null && $error === "" && !function_exists()57–59->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value !== false && $error !== null && $error === "" && !function_exists()59->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), get_option(), function_exists(), apply_filters()
$value !== false && !is_wp_error() && $error !== null && $error === "" && !function_exists()60->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), get_option(), function_exists(), apply_filters()
$value === false && $error !== null && $error === "" && !function_exists()60->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && !is_wp_error() && function_exists()60explode(), array_map(), array_filter(), array_map(), array_filter(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
!$value && $error !== null && $error === "" && !function_exists()60->strip_invalid_text_for_column(), is_wp_error(), __(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value !== false && is_wp_error() && $error !== null && $error !== "" && function_exists()61->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_wp_error() && !is_email() && $error !== null && $error === "" && function_exists()61->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
is_email() && $error !== null && $error === "" && !function_exists()61->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value !== false && !is_wp_error() && $error !== null && $error !== "" && function_exists()62->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && !function_exists()62->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
is_wp_error() && $error !== null && $error === "" && function_exists()63–65->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && $error !== null && $error === "" && function_exists()65->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && is_wp_error() && !function_exists()65explode(), array_map(), array_filter(), array_map(), array_filter(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value !== false && !is_wp_error() && $error !== null && $error === "" && function_exists()66->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value === false && $error !== null && $error === "" && function_exists()66->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
!$value && $error !== null && $error === "" && function_exists()66->strip_invalid_text_for_column(), is_wp_error(), __(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_email() && $error !== null && $error === "" && !function_exists()66->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
is_email() && $error !== null && $error === "" && function_exists()67->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$error !== null && $error === "" && function_exists()68->strip_invalid_text_for_column(), is_wp_error(), strip_tags(), wp_kses_data(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && is_wp_error() && $error !== null && $error === "" && !function_exists()70->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$value !== false && $error !== null && $error === "" && !function_exists()71->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), apply_filters()
$error !== null && $error === "" && is_wp_error() && function_exists()71explode(), array_map(), array_filter(), array_map(), array_filter(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
!is_email() && $error !== null && $error === "" && function_exists()72->strip_invalid_text_for_column(), is_wp_error(), sanitize_email(), is_email(), __(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && is_wp_error() && $error !== null && $error === "" && function_exists()76->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), ->get_error_message(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()
$value !== false && $error !== null && $error === "" && function_exists()77->strip_invalid_text_for_column(), wp_encode_emoji(), ->strip_invalid_text_for_column(), is_wp_error(), esc_html(), is_wp_error(), __(), ->get_error_code(), sprintf(), get_option(), function_exists(), add_settings_error(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev58420–77101
8.558420–77101
8.458420–7710138 fewer instructions than PHP 8.3
8.362220–77101
8.262220–771011 more instruction than PHP 8.1
8.162120–77101
7.462120–77101

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while sanitize_option() runs, in this order:

  1. apply_filters( sanitize_option_{$option} )filterline 5100 (+274 into the body)

    Filters an option value following sanitization.

Uses · 15

Show all 15

Used by · 8

Source code

function sanitize_option( $option, $value ) {	global $wpdb; 	$original_value = $value;	$error          = null; 	switch ( $option ) {		case 'admin_email':		case 'new_admin_email':			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );			if ( is_wp_error( $value ) ) {				$error = $value->get_error_message();			} else {				$value = sanitize_email( $value );				if ( ! is_email( $value ) ) {					$error = __( 'The email address entered did not appear to be a valid email address. Please enter a valid email address.' );				}			}			break; 		case 'thumbnail_size_w':		case 'thumbnail_size_h':		case 'medium_size_w':		case 'medium_size_h':		case 'medium_large_size_w':		case 'medium_large_size_h':		case 'large_size_w':		case 'large_size_h':		case 'mailserver_port':		case 'comment_max_links':		case 'page_on_front':		case 'page_for_posts':		case 'rss_excerpt_length':		case 'default_category':		case 'default_email_category':		case 'default_link_category':		case 'close_comments_days_old':		case 'comments_per_page':		case 'thread_comments_depth':		case 'users_can_register':		case 'start_of_week':		case 'site_icon':		case 'fileupload_maxk':			$value = absint( $value );			break; 		case 'posts_per_page':		case 'posts_per_rss':			$value = (int) $value;			if ( empty( $value ) ) {				$value = 1;			}			if ( $value < -1 ) {				$value = abs( $value );			}			break; 		case 'default_ping_status':		case 'default_comment_status':			// Options that if not there have 0 value but need to be something like "closed".			if ( '0' === (string) $value || '' === $value ) {				$value = 'closed';			}			break; 		case 'blogdescription':		case 'blogname':			$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );			if ( $value !== $original_value ) {				$value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', wp_encode_emoji( $original_value ) );			} 			if ( is_wp_error( $value ) ) {				$error = $value->get_error_message();			} else {				$value = esc_html( $value );			}			break; 		case 'blog_charset':

Changelog

Introduced in 2.0.5. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.