Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/sanitize key


The "sanitize_key" filter is used to filter the key from the sanitize_key() function output. That function sanitize a string key which are used as internal identifiers.

<?php add_filter( 'sanitize_key', 'filter_function_name' ); ?>

By default, lowercase alphanumeric characters, dashes and underscores are allowed. Uppercase characters will be converted to lowercase. After `sanitize_key()` has done its work, it passes the sanitized key through this `sanitize_key` filter.


This filter accepts two parameters, but at least one.

Filtering the output

The `sanitize_key()` function done its work, then you want to remove the dashes inside the key.

add_filter( 'sanitize_key', 'remove_dashes_from_keys' );

function remove_dashes_from_keys( $key ) {
    return preg_replace( '/-/s', '', $key );;

Filtering the raw key

You can interact with the raw key as second parameter of that filter. The raw key is the exact value passed to the function. You could use it to make your own sanitization of the original key.

add_filter( 'sanitize_key', 'do_something_with_rawkeys', 10, 2 );

function do_something_with_rawkeys( $key, $rawkey ) {
    // do domething with $rawkey
    return $rawkey;

Source File

sanitize_key() is located in wp-includes/formatting.php#L1231.

See Also

This page is marked as incomplete. You can help Codex by expanding it.