sanitize_option() – Sanitises various option values based on the nature of the option.

You appear to be a bot. Output may be restricted

Description

Sanitizes various option values based on the nature of the option.

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

Usage

$string = sanitize_option( $option, $value );

Parameters

$option
( string ) required – The name of the option.
$value
( string ) required – The unsanitised value.

Returns

string Sanitized value.

Source

File name: wordpress/wp-includes/formatting.php
Lines:

1 to 100 of 267
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':
      $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' == $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':
      $value = preg_replace( '/[^a-zA-Z0-9_-]/', '', $value ); // Strips slashes.
      break;

    case 'blog_public':
      // This is the value if the settings checkbox is not checked on POST. Don't rely on this.
      if ( null === $value ) {
        $value = 1;
      } else {
        $value = (int) $value;
      }
      break;

    case 'date_format':
    case 'time_format':
    case 'mailserver_url':
    case 'mailserver_login':
    case 'mailserver_pass':
    case 'upload_path':
      $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
      if ( is_wp_error( $value ) ) {
        $error = $value->get_error_message();
 

 View on GitHub View on Trac