Family Encyclopedia >> Electronics

How to Restrict WordPress Post Editing and Deletion After 30 Days

As experienced WordPress developers managing sites with multiple authors, we've seen the need for precise content controls. Editorial plugins help, but custom solutions often provide the best fit. Recently, we assisted a client in implementing restrictions: after 30 days, only admins can edit or delete published posts, protecting content integrity while allowing editors flexibility on recent work. Here's how to achieve this reliably.

How to Restrict WordPress Post Editing and Deletion After 30 Days

Add this proven code snippet to your theme's functions.php file or a custom site plugin. It's battle-tested across numerous client sites for secure, multi-author environments.

function wpbeginner_restrict_editing( $allcaps, $cap, $args ) {

    // Bail out if we're not asking to edit or delete a post...
    if ( 'edit_post' != $args[0] && 'delete_post' != $args[0]
    // ...or the user is an admin
    || ! empty( $allcaps['manage_options'] )
    // ...or the user can't edit posts anyway
    || empty( $allcaps['edit_posts'] ) ) return $allcaps;

    // Load post data:
    $post = get_post( $args[2] );

    // Skip if the post is not published:
    if ( 'publish' != $post->post_status ) return $allcaps;

    // If the post is older than 30 days. Change this to suit your needs.
    if ( strtotime( $post->post_date ) < strtotime( '-30 days' ) ) {
        // Then disallow editing.
        $allcaps[ $cap[0] ] = false;
    }

    return $allcaps;
}
add_filter( 'user_has_cap', 'wpbeginner_restrict_editing', 10, 3 );

This function evaluates user capabilities for editing or deleting posts. It verifies the post is published and exceeds 30 days old before revoking access for non-admins. Editors retain full control over newer content, while admins bypass all restrictions—ensuring site stability without overcomplicating workflows.

We've deployed this on production sites to prevent accidental changes on evergreen content. Ideal for blogs, news sites, or teams. Thoughts on use cases? Share in the comments.

Source:
Smhmic