sanitize_key() WordPress Function

The sanitize_key() function is a built-in WordPress function that is used to clean up a string before it is used as a key. This function is useful for making sure that a string is safe to use as a key in an array or a database.

sanitize_key( string $key ) #

Sanitizes a string key.


Description

Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes, and underscores are allowed.


Top ↑

Parameters

$key

(string)(Required)String key.


Top ↑

Return

(string) Sanitized key.


Top ↑

Source

File: wp-includes/formatting.php

2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
function sanitize_key( $key ) {
    $sanitized_key = '';
 
    if ( is_scalar( $key ) ) {
        $sanitized_key = strtolower( $key );
        $sanitized_key = preg_replace( '/[^a-z0-9_\-]/', '', $sanitized_key );
    }
 
    /**
     * Filters a sanitized key string.
     *
     * @since 3.0.0
     *
     * @param string $sanitized_key Sanitized key.
     * @param string $key           The key prior to sanitization.
     */
    return apply_filters( 'sanitize_key', $sanitized_key, $key );
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.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