Hi, my name is
Lee Harris and
I'm a Freelance Web Designer

Prevent pages or posts from being deleted in WordPress

9.Aug.2022 | ,

To stop any page from being deleted by any user role, including admins, add the following code to your functions.php file.

Update the $post_id variable to the ID of the page or post you want to protect from deletion.

function prevent_views($allcaps, $caps, $args) {
    $post_id = 6293;
    if ( isset( $args[0] ) && isset( $args[2] ) && $args[2] == $post_id && $args[0] == 'delete_post' ) {
        $allcaps[ $caps[0] ] = false;
    }
    return $allcaps;
}
add_filter ('user_has_cap', 'prevent_views', 10, 3);

, , ,

  • html5
  • css3
  • sass
  • php
  • WordPress
  • WooCommerce