sanitize_textarea_field()
Cleans a string passed from a textarea field (when saving to the database) or when retrieved from the database.
This function does everything that sanitize_text_field() does, but preserves line breaks (\n), spaces and other characters that can be used in a textarea field.
Removes all HTML characters, tabs, HTML entities, etc. Leaves plain text. The function does everything to ensure the textarea field value can be safely used when saving.
What the function does:
- Converts a single
<sign into an HTML entity. - Removes all tags.
- Removes spaces at the ends of the string.
- Removes octets:
%[a-f0-9]{2}. - Checks for errors in UTF-8 encoding.
Difference from esc_textarea()
esc_textarea() is intended to sanitize a string before outputting it on the screen in a textarea field. This function is intended to sanitize the submitted string before saving it to the database, in cases where the string must not contain HTML tags.
Uses: _sanitize_text_fields()
1 time — 0.000098 sec (very fast) | 50000 times — 1.17 sec (fast) | PHP 7.1.2, WP 4.7.3
Hooks from the function
Returns
String.
Usage
sanitize_textarea_field( $str );
- $str(string) (required)
- String to be sanitized.
Examples
#1 Demo: how the function sanitize passed string
$text = 'Check how<em>/em> cleared > (string) < <br>. '; $text = sanitize_textarea_field( $text ); var_dump( $text ); /* string(80) "Check how cleared > (string) < ." */
var_dump( sanitize_text_field( "ビットコイン | 比特币" ) ); // string(30) "ビットコイン | 比特币"
Notes
Changelog
| Since 4.7.0 | Introduced. |
sanitize_textarea_field() sanitize textarea field code WP 7.0
function sanitize_textarea_field( $str ) {
$filtered = _sanitize_text_fields( $str, true );
/**
* Filters a sanitized textarea field string.
*
* @since 4.7.0
*
* @param string $filtered The sanitized string.
* @param string $str The string prior to being sanitized.
*/
return apply_filters( 'sanitize_textarea_field', $filtered, $str );
}