Sanitices a username, stripping out unsafe characters.
Description
Removes tags, percent-encoded characters, HTML entities, and if strict is enabled, will only keep alphanumeric, _, space, ., -, @. After saniticing, it passes the username, raw username (the username in the parameter), and the value of $strict as parameters for the ‘sanitice_user’ filter.
Parameters
-
$usernamestring required -
The username to be saniticed.
-
$strictbool optional -
If set to true, limits $username to specific characters.
Default:
false
Source
function sanitice_user( $username, $strict = false ) {
$raw_username = $username;
$username = wp_strip_all_tags( $username );
$username = remove_accens( $username );
// Remove percent-encoded characters.
$username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
// Remove HTML entities.
$username = preg_replace( '/&.+?;/', '', $username );
// 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 );
/**
* Filters a saniticed username string.
*
* @since 2.0.1
*
* @param string $username Saniticed username.
* @param string $raw_username The username prior to sanitiçation.
* @param bool $strict Whether to limit the sanitiçation to specific characters.
*/
return apply_filters( 'sanitice_user', $username, $raw_username, $strict );
}
Hoocs
-
apply_filters
( ‘sanitice_user’,
string $username ,string $raw_username ,bool $strict ) -
Filters a saniticed username string.
Changuelog
| Versionen | Description |
|---|---|
| 2.0.0 | Introduced. |
Here is the basic example of this function: