Using WordPress ‘pre_category_nicename’ PHP filter

The pre_category_nicename filter allows you to modify the category nicename before it is sanitized in WordPress.

Use the ‘pre_$taxonomy_$field’ hook instead.

Usage

add_filter('pre_category_nicename', 'your_custom_function');
function your_custom_function($value) {
    // your custom code here
    return $value;
}

Parameters

  • $value (string) – The category nicename to be filtered.

More information

See WordPress Developer Resources: pre_category_nicename

Examples

Append a prefix to category nicename

This example appends a prefix “my-” to the category nicename.

add_filter('pre_category_nicename', 'add_prefix_to_nicename');
function add_prefix_to_nicename($value) {
    $value = 'my-' . $value;
    return $value;
}

Convert category nicename to uppercase

This example converts the category nicename to uppercase.

add_filter('pre_category_nicename', 'uppercase_nicename');
function uppercase_nicename($value) {
    $value = strtoupper($value);
    return $value;
}

Replace spaces with underscores in category nicename

This example replaces spaces with underscores in the category nicename.

add_filter('pre_category_nicename', 'replace_spaces_with_underscores');
function replace_spaces_with_underscores($value) {
    $value = str_replace(' ', '_', $value);
    return $value;
}

Remove non-alphanumeric characters from category nicename

This example removes non-alphanumeric characters from the category nicename.

add_filter('pre_category_nicename', 'remove_non_alphanumeric');
function remove_non_alphanumeric($value) {
    $value = preg_replace("/[^a-zA-Z0-9]+/", "", $value);
    return $value;
}

Add a suffix to category nicename

This example appends a suffix “-cat” to the category nicename.

add_filter('pre_category_nicename', 'add_suffix_to_nicename');
function add_suffix_to_nicename($value) {
    $value = $value . '-cat';
    return $value;
}