sanitize_text_field

Definition:
function sanitize_text_field($str) {}

Sanitize a string from user input or from the db
check for invalid UTF-8, Convert single < characters to entity, strip all tags, remove line breaks, tabs and extra white space, strip octets.

Parameters

  • string $str

Defined filters

  • sanitize_text_field
    apply_filters('sanitize_text_field', $filtered, $str)

Source code

function sanitize_text_field($str) {

	$filtered = wp_check_invalid_utf8( $str );



	if ( strpos($filtered, '<') !== false ) {

		$filtered = wp_pre_kses_less_than( $filtered );

		// This will strip extra whitespace for us.

		$filtered = wp_strip_all_tags( $filtered, true );

	} else {

		$filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) );

	}



	$match = array();

	$found = false;

	while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) {

		$filtered = str_replace($match[0], '', $filtered);

		$found = true;

	}



	if ( $found ) {

		// Strip out the whitespace that may now exist after removing the octets.

		$filtered = trim( preg_replace('/ +/', ' ', $filtered) );

	}



	return apply_filters('sanitize_text_field', $filtered, $str);

}

2789

sanitize_term_field

Definition:
function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {}

Cleanse the field value in the term based on the context.
Passing a term field value through the function should be assumed to have cleansed the value for whatever context the term field is going to be used.

Parameters

  • string $field: Term field to sanitize
  • string $value: Search for this term value
  • int $term_id: Term ID
  • string $taxonomy: Taxonomy Name
  • string $context: Either edit, db, display, attribute, or js.

Return values

returns:sanitized field

Defined filters

  • edit_term_{$field}
    apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy)
  • edit_{$taxonomy}_{$field}
    apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id)

Source code

function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {

	if ( 'parent' == $field  || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {

		$value = (int) $value;

		if ( $value < 0 )

			$value = 0;

	}



	if ( 'raw' == $context )

		return $value;



	if ( 'edit' == $context ) {

		$value = apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy);

		$value = apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id);

2787

sanitize_term

Definition:
function sanitize_term($term, $taxonomy, $context = 'display') {}

Sanitize Term all fields.
Relies on sanitize_term_field() to sanitize the term. The difference is that this function will sanitize <strong>all</strong> fields. The context is based on sanitize_term_field().

Parameters

  • array|object $term: The term to check
  • string $taxonomy: The taxonomy name to use
  • string $context: Default is ‘display’.

Return values

returns:with all fields sanitized

Source code

function sanitize_term($term, $taxonomy, $context = 'display') {



	if ( 'raw' == $context )

		return $term;



	$fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');



	$do_object = false;

	if ( is_object($term) )

		$do_object = true;



	$term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);



	foreach ( (array) $fields as $field ) {

		if ( $do_object ) {

			if ( isset($term->$field) )

				$term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);

		} else {

			if ( isset($term[$field]) )

				$term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);

		}

	}



	if ( $do_object )

		$term->filter = $context;

	else

		$term['filter'] = $context;



	return $term;

}

2785

sanitize_sql_orderby

Definition:
function sanitize_sql_orderby( $orderby ){

Ensures a string is a valid SQL order by clause.
Accepts one or more columns, with or without ASC/DESC, and also accepts RAND().

Parameters

  • string $orderby: Order by string to be checked.

Return values

returns:Returns the order by clause if it is a match, false otherwise.

Source code

function sanitize_sql_orderby( $orderby ){

	preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);

	if ( !$obmatches )

		return false;

	return $orderby;

}

2783

sanitize_post_field

Definition:
function sanitize_post_field($field, $value, $post_id, $context) {}

Sanitize post 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 Post Object field name.
  • mixed $value: The Post Object value.
  • int $post_id: Post ID.
  • string $context: How to sanitize post fields. Looks for ‘raw’, ‘edit’, ‘db’, ‘display’, ‘attribute’ and ‘js’.

Return values

returns:Sanitized value.

Defined filters

  • edit_{$field}
    apply_filters("edit_{$field}", $value, $post_id)
  • {$field_no_prefix}_edit_pre
    apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id)

Source code

function sanitize_post_field($field, $value, $post_id, $context) {

	$int_fields = array('ID', 'post_parent', 'menu_order');

	if ( in_array($field, $int_fields) )

		$value = (int) $value;



	// Fields which contain arrays of ints.

	$array_int_fields = array( 'ancestors' );

	if ( in_array($field, $array_int_fields) ) {

		$value = array_map( 'absint', $value);

		return $value;

	}



	if ( 'raw' == $context )

		return $value;



	$prefixed = false;

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

		$prefixed = true;

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

	}



	if ( 'edit' == $context ) {

		$format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');



		if ( $prefixed ) {

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

			// Old school

			$value = apply_filters("{$field_no_prefix}_edit_pre", $value, $post_id);

		} else {

2781