sanitize_text_field() WordPress Function

The sanitize_text_field() function is a built-in function in WordPress that is used to clean up data before saving it to the database. It is used to remove unwanted characters from a string, such as special characters, HTML tags, and excessive whitespace. This function can be used on both the front-end and back-end of WordPress.

sanitize_text_field( string $str ) #

Sanitizes a string from user input or from the database.


Description

  • Checks for invalid UTF-8,
  • Converts single < characters to entities
  • Strips all tags
  • Removes line breaks, tabs, and extra whitespace
  • Strips octets

Top ↑

See also


Top ↑

Parameters

$str

(string)(Required)String to sanitize.


Top ↑

Return

(string) Sanitized string.


Top ↑

More Information

Basic Usage

<?php sanitize_text_field( $str ) ?>

Top ↑

Source

File: wp-includes/formatting.php

function sanitize_text_field( $str ) {
	$filtered = _sanitize_text_fields( $str, false );

	/**
	 * Filters a sanitized text field string.
	 *
	 * @since 2.9.0
	 *
	 * @param string $filtered The sanitized string.
	 * @param string $str      The string prior to being sanitized.
	 */
	return apply_filters( 'sanitize_text_field', $filtered, $str );
}


Top ↑

Changelog

Changelog
VersionDescription
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More