# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Admin](https://wpide.com/library/category/admin/)
- Tags: slugs, permalinks, editorial
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/admin-lock-published-slugs/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Lock the URL Slug of Published Posts" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
