phpwordpressfunctioncustom-wordpress-pageswordpress-admin

How to disable delete option for a specific page in Wordpress admin?


I found many questions like this but nothing matching to my requirement.Here my requirement is to lock a specific page named settings. It's not to be deleted by others.But it should be able to edit. Is there any way to lock a specific page using its page id or name.


Solution

  • Create a hook in themes function file as per below:

    function restrict_page_deletion($post_ID){
        $user = get_current_user_id();
    
        $restricted_pageId = 4;
    
        if($post_ID == $restricted_pageId)
        {
            echo "You are not authorized to delete this page.";
            exit;
        }
    }
    add_action('before_delete_post', 'restrict_page_deletion', 10, 1);
    

    Pass your page id to a restricted_pageId variable.

    If you want to implement this functionality for multiple pages then use the array in place of the variable.

    Admin can move a page to trash but admin will not able to delete it.

    If you want to block admin for trach functionality then call a hook on "wp_trash_post" action.

    add_action('wp_trash_post', 'restrict_page_deletion', 10, 1);