0

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.

4

2 回答 2

3

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);
于 2018-08-29T06:22:16.390 回答
2
function wpse_312694_restrict_page_deletion( $caps, $cap, $user_id, $args ) {
$post_id = $args[0];

if ( $cap === 'delete_post' && $post_id === 117 ) {
    $caps[] = 'do_not_allow';
}

return $caps;
}
add_filter( 'map_meta_cap', 'wpse_312694_restrict_page_deletion', 10, 4 );
于 2021-08-16T19:42:18.800 回答