Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
prevent users from saving pages with reserved rewrite rules as slugs
<?php
// prevent users from saving pages with reserved rewrite rules as slugs
// includes post types, feeds, and search
function my_reserved_slugs() {
return array(
'post',
'attachment',
'category',
'tag',
'format',
'feed',
'atom',
'rss',
'json',
'search',
'people',
'course',
'publication',
'facility',
'research'
);
}
add_filter( 'wp_unique_post_slug_is_bad_hierarchical_slug', 'my_reserved_hierarchical_slugs', 10, 4 );
function my_reserved_hierarchical_slugs( $is_bad_hierarchical_slug, $slug, $post_type, $post_parent ) {
$reserved = my_reserved_slugs();
if ( !$post_parent && in_array( $slug, $reserved ) )
return true;
return $is_bad_hierarchical_slug;
}
add_filter( 'wp_unique_post_slug_is_bad_flat_slug', 'my_reserved_flat_slugs', 10, 3 );
function my_reserved_flat_slugs( $is_bad_flat_slug, $slug, $post_type ) {
$reserved = my_reserved_slugs();
if ( in_array( $slug, $reserved ) )
return true;
return $is_bad_flat_slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.