sanitize_user_field

Definition:
function sanitize_user_field($field, $value, $user_id, $context) {}

Sanitize user field based on context.
Possible context values are: ‘raw’, ‘edit’, ‘db’, ‘display’, ‘attribute’ and ‘js’. The ‘display’ context is used by default. ‘attribute’ and ‘js’ contexts are treated like ‘display’ when calling filters.

Parameters

  • string $field: The user Object field name.
  • mixed $value: The user Object value.
  • int $user_id: user ID.
  • string $context: How to sanitize user fields. Looks for ‘raw’, ‘edit’, ‘db’, ‘display’, ‘attribute’ and ‘js’.

Return values

returns:Sanitized value.

Defined filters

  • edit_{$field}
    apply_filters("edit_{$field}", $value, $user_id)
  • edit_user_{$field}
    apply_filters("edit_user_{$field}", $value, $user_id)

Source code

function sanitize_user_field($field, $value, $user_id, $context) {

	$int_fields = array('ID');

	if ( in_array($field, $int_fields) )

		$value = (int) $value;



	if ( 'raw' == $context )

		return $value;



	if ( !is_string($value) && !is_numeric($value) )

		return $value;



	$prefixed = false;

	if ( false !== strpos($field, 'user_') ) {

		$prefixed = true;

		$field_no_prefix = str_replace('user_', '', $field);

	}



	if ( 'edit' == $context ) {

		if ( $prefixed ) {

			$value = apply_filters("edit_{$field}", $value, $user_id);

		} else {

			$value = apply_filters("edit_user_{$field}", $value, $user_id);

		}

2799

sanitize_user

Definition:
function sanitize_user( $username, $strict = false ) {}

Sanitize username stripping out unsafe characters.
Removes tags, octets, entities, and if strict is enabled, will only keep alphanumeric, _, space, ., -, @. After sanitizing, it passes the username, raw username (the username in the parameter), and the value of $strict as parameters for the ‘sanitize_user’ filter.

Parameters

  • string $username: The username to be sanitized.
  • bool $strict: If set limits $username to specific characters. Default false.

Return values

returns:The sanitized username, after passing through filters.

Defined filters

  • sanitize_user
    apply_filters( 'sanitize_user', $username, $raw_username, $strict )

Source code

function sanitize_user( $username, $strict = false ) {

	$raw_username = $username;

	$username = wp_strip_all_tags( $username );

	$username = remove_accents( $username );

	// Kill octets

	$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );

	$username = preg_replace( '/&.+?;/', '', $username ); // Kill entities



	// If strict, reduce to ASCII for max portability.

	if ( $strict )

		$username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );



	$username = trim( $username );

	// Consolidate contiguous whitespace

	$username = preg_replace( '|\s+|', ' ', $username );



	return apply_filters( 'sanitize_user', $username, $raw_username, $strict );

}

2797

sanitize_url

Definition:
function sanitize_url( $url, $protocols = null ) {}

Performs esc_url() for database or redirect usage.

Parameters

  • string $url: The URL to be cleaned.
  • array $protocols: An array of acceptable protocols.

Return values

returns:The cleaned URL.

Source code

function sanitize_url( $url, $protocols = null ) {

	_deprecated_function( __FUNCTION__, '2.8', 'esc_url_raw()' );

	return esc_url_raw( $url, $protocols );

}

2795

sanitize_title_with_dashes

Definition:
function sanitize_title_with_dashes($title, $raw_title = '', $context = 'display') {}

Sanitizes title, replacing whitespace and a few other characters with dashes.
Limits the output to alphanumeric characters, underscore (_) and dash (-). Whitespace becomes a dash.

Parameters

  • string $title: The title to be sanitized.
  • string $raw_title: Optional. Not used.
  • string $context: Optional. The operation for which the string is sanitized.

Return values

returns:The sanitized title.

Source code

function sanitize_title_with_dashes($title, $raw_title = '', $context = 'display') {

	$title = strip_tags($title);

	// Preserve escaped octets.

	$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);

	// Remove percent signs that are not part of an octet.

	$title = str_replace('%', '', $title);

	// Restore octets.

	$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);



	if (seems_utf8($title)) {

		if (function_exists('mb_strtolower')) {

			$title = mb_strtolower($title, 'UTF-8');

		}

		$title = utf8_uri_encode($title, 200);

	}



	$title = strtolower($title);

	$title = preg_replace('/&.+?;/', '', $title); // kill entities

	$title = str_replace('.', '-', $title);



	if ( 'save' == $context ) {

		// nbsp, ndash and mdash

		$title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );

		// iexcl and iquest

		$title = str_replace( array( '%c2%a1', '%c2%bf' ), '', $title );

		// angle quotes

		$title = str_replace( array( '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba' ), '', $title );

		// curly quotes

		$title = str_replace( array( '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d' ), '', $title );

		// copy, reg, deg, hellip and trade

		$title = str_replace( array( '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2' ), '', $title );

	}



	$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);

	$title = preg_replace('/\s+/', '-', $title);

	$title = preg_replace('|-+|', '-', $title);

	$title = trim($title, '-');



	return $title;

}

2793

sanitize_title

Definition:
function sanitize_title($title, $fallback_title = '', $context = 'save') {}

Sanitizes title or use fallback title.
Specifically, HTML and PHP tags are stripped. Further actions can be added via the plugin API. If $title is empty and $fallback_title is set, the latter will be used.

Parameters

  • string $title: The string to be sanitized.
  • string $fallback_title: Optional. A title to use if $title is empty.
  • string $context: Optional. The operation for which the string is sanitized

Return values

returns:The sanitized string.

Defined filters

  • sanitize_title
    apply_filters('sanitize_title', $title, $raw_title, $context)

Source code

function sanitize_title($title, $fallback_title = '', $context = 'save') {

	$raw_title = $title;



	if ( 'save' == $context )

		$title = remove_accents($title);



	$title = apply_filters('sanitize_title', $title, $raw_title, $context);



	if ( '' === $title || false === $title )

		$title = $fallback_title;



	return $title;

}

2791