sanitize_option() – Sanitizes 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 unsanitized value.

Returns

string Sanitized value.

Source

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

201 to 276 of 276
      }
      break;

    case 'timezone_string':
      $allowed_zones = timezone_identifiers_list( DateTimeZone::ALL_WITH_BC );
      if ( ! in_array( $value, $allowed_zones, true ) && ! empty( $value ) ) {
        $error = __( 'The timezone you have entered is not valid. Please select a valid timezone.' );
      }
      break;

    case 'permalink_structure':
    case 'category_base':
    case 'tag_base':
      $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_url( $value );
        $value = str_replace( 'http://', '', $value );
      }

      if ( 'permalink_structure' === $option && null === $error
        && '' !== $value && ! preg_match( '/%[^\/%]+%/', $value )
      ) {
        $error = sprintf(
          /* translators: %s: Documentation URL. */
          __( 'A structure tag is required when using custom permalinks. <a href="%s">Learn more</a>' ),
          __( 'https://wordpress.org/documentation/article/customize-permalinks/#choosing-your-permalink-structure' )
        );
      }
      break;

    case 'default_role':
      if ( ! get_role( $value ) && get_role( 'subscriber' ) ) {
        $value = 'subscriber';
      }
      break;

    case 'moderation_keys':
    case 'disallowed_keys':
      $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
      if ( is_wp_error( $value ) ) {
        $error = $value->get_error_message();
      } else {
        $value = explode( "\n", $value );
        $value = array_filter( array_map( 'trim', $value ) );
        $value = array_unique( $value );
        $value = implode( "\n", $value );
      }
      break;
  }

  if ( null !== $error ) {
    if ( '' === $error && is_wp_error( $value ) ) {
      /* translators: 1: Option name, 2: Error code. */
      $error = sprintf( __( 'Could not sanitize the %1$s option. Error code: %2$s' ), $option, $value->get_error_code() );
    }

    $value = get_option( $option );
    if ( function_exists( 'add_settings_error' ) ) {
      add_settings_error( $option, "invalid_{$option}", $error );
    }
  }

  
/**
 * Filters an option value following sanitization.
 *
 * @since 2.3.0
 * @since 4.3.0 Added the `$original_value` parameter.
 *
 * @param string $value          The sanitized option value.
 * @param string $option         The option name.
 * @param string $original_value The original value passed to the function.
 */
  return apply_filters( "sanitize_option_{$option}", $value, $option, $original_value );
}
 

 View on GitHub View on Trac