Skip navigation

How to Define Reserve Slugs for WordPress Posts and Pages

This blog post was migrated from one of my older blogs. Let me know if it has any issues, missing assets, or outdated content.

This post was inspired by an answer I posted in the WordPress Stack Exchange.

This is an interesting WordPress problem that could span several scenarios. But let’s say you have a custom post type named ‘songs’ and you defined its slug as ‘songs’ so its archive page URL is http://www.mysite.com/songs.

This is fine and all but a slug used for a custom post type archive page, i.e. ‘songs’, is not saved in the database and is, therefore, not available when you’re creating/editing posts to tell WordPress “NO! The slug ‘songs’ is taken!”. With that said, a user could come along and create a post (or page) with the slug ‘songs’ which would therefore have the same URL as your custom post type archive page: http://www.mysite.com/songs.

So how do you keep users on your site from creating posts with particular slugs, a.k.a. reserve slugs?

It’s pretty simple, actually. Use one, or both, of the following two filters. They hook into WordPress when it’s checking a post’s slug, allowing you to return “true” which tells WordPress that the post slug is bad. If you return “true”, WordPress adds on a suffix, just like it would do if you were trying to use a slug that is already taken.

The first filter, ‘wp_unique_post_slug_is_bad_hierarchical_slug’, is for hierarchical posts and the second filter, ‘wp_unique_post_slug_is_bad_flat_slug’, is for non-hierarchical posts. While both filters provide the post’s $slug and $post_type, the hierarchical filter also provides the ID for the post parent so if the $post_parent is 0, you know this is a “base” post.

add_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'rachel_cherry_is_bad_hierarchical_slug', 10, 4 );
function rachel_cherry_is_bad_hierarchical_slug( $is_bad_hierarchical_slug, $slug, $post_type, $post_parent ) {
   // This post has no parent and is a "base" post
   if ( !$post_parent && $slug == 'songs' )
      return true;
   return $is_bad_hierarchical_slug;
}
add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'rachel_cherry_is_bad_flat_slug', 10, 3 );
function rachel_cherry_is_bad_flat_slug( $is_bad_flat_slug, $slug, $post_type ) {
   if ( $slug == 'songs' )
      return true;
   return $is_bad_flat_slug;
}

These filters can be found in the WordPress function wp_unique_post_slug() in the wp-includes/post.php file.

One Reply to “How to Define Reserve Slugs for WordPress Posts and Pages”

Leave a Reply

Your email address will not be published. Required fields are marked *

Login to WordPress