Created
September 1, 2017 15:32
-
-
Save stephanieleary/5073d7c89677b33e2d802ac1825ac77d to your computer and use it in GitHub Desktop.
prevent users from saving pages with reserved rewrite rules as slugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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