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

1
<?php sanitize_text_field( $str ) ?>

Top ↑

Source

File: wp-includes/formatting.php

5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
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