Using WordPress ‘editable_slug’ PHP filter

The editable_slug WordPress PHP filter allows you to modify the editable slug for a post or term.

Usage

add_filter('editable_slug', 'your_custom_function', 10, 2);

function your_custom_function($slug, $tag) {
    // your custom code here
    return $slug;
}

Parameters

  • $slug (string) – The editable slug. It will be either a term slug or post URI depending on the context in which it is evaluated.
  • $tag (WP_Term|WP_Post) – Term or post object.

More information

See WordPress Developer Resources: editable_slug

Examples

Add prefix to post slugs

Add a prefix to the post slugs.

add_filter('editable_slug', 'add_prefix_to_post_slugs', 10, 2);

function add_prefix_to_post_slugs($slug, $tag) {
    if ($tag instanceof WP_Post) {
        $slug = 'prefix-' . $slug;
    }
    return $slug;
}

Add suffix to term slugs

Add a suffix to the term slugs.

add_filter('editable_slug', 'add_suffix_to_term_slugs', 10, 2);

function add_suffix_to_term_slugs($slug, $tag) {
    if ($tag instanceof WP_Term) {
        $slug .= '-suffix';
    }
    return $slug;
}

Change all slugs to uppercase

Modify all slugs to be uppercase.

add_filter('editable_slug', 'slug_to_uppercase', 10, 2);

function slug_to_uppercase($slug, $tag) {
    return strtoupper($slug);
}

Replace spaces with underscores in slugs

Replace spaces with underscores in slugs.

add_filter('editable_slug', 'replace_spaces_with_underscores', 10, 2);

function replace_spaces_with_underscores($slug, $tag) {
    return str_replace(' ', '_', $slug);
}

Remove numbers from slugs

Remove numbers from the slugs.

add_filter('editable_slug', 'remove_numbers_from_slugs', 10, 2);

function remove_numbers_from_slugs($slug, $tag) {
    return preg_replace('/\d/', '', $slug);
}