sanitize_key( string $key ): string

Sanitizes a string key.

Description

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

Parameters

$keystringrequired
String key.

Return

string Sanitized key.

Source

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 );
}

Hooks

apply_filters( ‘sanitize_key’, string $sanitized_key, string $key )

Filters a sanitized key string.

Changelog

VersionDescription
3.0.0Introduced.

User Contributed Notes

  1. Skip to note 2 content
    <?php
    $sankey = sanitize_key('Testexample1-_/[]{}');
    echo $sankey;
    ?>

    In above code use one uppercase & other lowercase letters, number(1), dash, underscore, forward slash, brackets. So sanitize_key() function change one uppercase letter into lowercase, remove forward slash & brackets.

    Output:
    testexample1-_

You must log in before being able to contribute a note or feedback.