How to disable page delete

My client is not a computer person. I created a website for him. There is some important pages. My client is always delete this page. Then i want to reconfigure the code (the page id).

How do i disable delete options for particular pages.

Read More

PS: He may able to EDIT these page. Not Delete.

Related posts

Leave a Reply

6 comments

  1. You can remove the capabilites delete_pages, delete_others_pages and delete_published_pages from the role which the user is assigned to. This will prevent the complete user role from deleting pages.

    To restrict this behavior only to one user, you have to create and assign a dedicated, new role to the user. Look at the Members plugin from Justin Tadlock for more information.

    Example: Remove the ability to delete pages from admin role

    $role = get_role('administrator');
    $role->remove_cap('delete_pages');
    $role->remove_cap('delete_others_pages');
    $role->remove_cap('delete_published_pages');
    

    More resources

  2. Brian Fegter’s answer is almost perfect.

    In my testing his answer will only work if you change the actions to “wp_trash_post” and “before_delete_post”

    function restrict_post_deletion($post_ID){
        $user = get_current_user_id();
        $restricted_users = array(21,25,54,2,19);
        $restricted_pages = array(2,21,52,64);
        if(in_array($user, $restricted_users) && in_array($post_ID, $restricted_pages)){
            echo "You are not authorized to delete this page.";
            exit;
        }
    }
    add_action('wp_trash_post', 'restrict_post_deletion', 10, 1);
    add_action('before_delete_post', 'restrict_post_deletion', 10, 1);
    
  3. You can create an action to restrict users from trashing or deleting posts as follows. It’s not the prettiest, but it works. You will have to manually populate the user and page ID’s.

    function restrict_post_deletion($post_ID){
        $user = get_current_user_id();
        $restricted_users = array(21,25,54,2,19);
        $restricted_pages = array(2,21,52,64);
        if(in_array($user, $restricted_users) && in_array($post_ID, $restricted_pages)){
            echo "You are not authorized to delete this page.";
            exit;
        }
    }
    add_action('trash_post', 'restrict_post_deletion', 10, 1);
    add_action('delete_post', 'restrict_post_deletion', 10, 1);
    
  4. Here’s an example that I tested and found working of how to prevent deletion of a specific post or page by its’ ID. It should work for all wordpress users:

    add_action('wp_trash_post', 'prevent_post_deletion');
    function prevent_post_deletion($postid){
        $protected_post_id = 67586;
        if ($postid == $protected_post_id) {
            exit('The page you were trying to delete is protected.');
        }
    }
    
  5. You could very easily hide the trash links from both the quick edit and the meta box. DO one using styles and the other using post_row_actions filter (you may be able to do both sith the filter – dunno)
    https://developer.wordpress.org/reference/hooks/post_row_actions/

    //hide meta with styles
    function hide_publishing_actions()
    {
        global $post;
        if ($post_id == 21)
        {
            if (!current_user_can('administrator'))
            {
                // stuff here for non-admins
                echo '<style type="text/css">#delete-action</style>';
            }
        }
    }
    
    add_action('admin_head-post.php', 'hide_publishing_actions');
    add_action('admin_head-post-new.php', 'hide_publishing_actions');
    
    //for quick edit
    add_filter('post_row_actions', 'remove_row_actions', 10, 1);
    function remove_row_actions($actions)
    {
        if (!current_user_can('administrator'))
        {
            if ($post_id == 21)
            unset($actions['edit']);
            unset($actions['view']);
            unset($actions['trash']);
            unset($actions['inline hide-if-no-js']);
        }
        return $actions;
    }
    
  6. You could also use the filter page_row_actions (see https://developer.wordpress.org/reference/hooks/page_row_actions/) which is called for every page in the page list table.

    You have to check the id of the post you don’t want to be edited or delete you can modify the action array for these pages. If your return an empty array, the page will still be listed in the page list table, but there are not links for edit,delete,…
    Therefore the page could not be edited or deleted.

    Your function should like this (Note: In my example code only the site admin has the right to modify the special page):

    function custom_filter_page_row_actions($actions,$post){
      $page_to_exclude = get_page_by_path(Slug of the page you want to exclude);
      $site_admin_email = get_option('admin_email'); // Get the email of the site admin
      $site_admin =  get_user_by('email',$site_admin_email); // Get WP_User-instance of the site admin
      if(($post == $page_to_exclude) && ($site_admin != wp_get_current_user())){
        $actions = array(); //remove all actions
      }
      return $actions;
    }
    add_filter('page_row_actions','custom_filter_page_row_actions',999999,2); //Use a high number for this filter so that it is called after all plugins e.g. WPBakery Pagebuilder has add there actions