What user roles should have wp_unique_post_slug_is_bad_flat_slug filter applied?

I have a plugin where I need to reserve some slugs so I am using the filters wp_unique_post_slug_is_bad_flat_slug and wp_unique_post_slug_is_bad_hierarchical_slug. I apply them like this on my plugin code:

if ( is_admin() ) {
    add_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'is_bad_hierarchical_slug', 10, 4 );
    add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'is_bad_flat_slug', 10, 3 );
}

Is using is_admin() correct? Or should I check for other user roles or just not check for anything and just apply them always?

1 Answer
1

Actually you should skip the condition altogether – you don’t need to worry about permissions or context here, you are merely saying:

If WordPress, for whatever reason, is determining if a slug is bad: run my additional conditions

Currently, if a theme/front-end plugin/REST API call was triggering the filter (i.e. insert/updating a post), your checks aren’t going to run (is_admin() is false) – just always attach the filter and let WordPress take care of the rest.

Leave a Comment