Lock the URL Slug of Published Posts
Prevents authors and editors from changing the slug of a post or page once it is published, so shared links and search rankings do not break. Administrators can still change it.
Opens WPIDE on
Code
// Users with this capability may still change slugs.
$wpide_slug_unlock_cap = 'manage_options';
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) use ( $wpide_slug_unlock_cap ) {
if ( empty( $postarr['ID'] ) || current_user_can( $wpide_slug_unlock_cap ) ) {
return $data;
}
$current = get_post( (int) $postarr['ID'] );
if ( $current && 'publish' === $current->post_status && '' !== $current->post_name && 'publish' === $data['post_status'] ) {
$data['post_name'] = $current->post_name;
}
return $data;
}, 10, 2 );
Notes
Change $wpide_slug_unlock_cap to choose who may still edit slugs.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.