Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
add comment

4 Answers

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.');
    }
}
share|improve this answer
    
This runs just before trying to trash a post, not to delete it. There is a different action for that too, as far as I know. –  Shahar Dekel Dec 4 '13 at 10:35
add comment

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);
share|improve this answer
    
Thank you, i did some tests too. –  Gabriel Jul 11 '13 at 12:25
add comment

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);
share|improve this answer
    
Brian Fegter, Thanks for the reply. I past the code in function.php. But how can i pass the Page Id? –  user8917 Sep 26 '11 at 8:38
    
The page ID is defined as $post_ID in the code example. –  Brian Fegter Sep 26 '11 at 12:09
    
My code example checks the current page/post id against the restricted_pages variable. It also checks the current user agains the restricted_users variable. To get this to work, you will have to manually add $restricted_users and $restricted_pages. –  Brian Fegter Sep 26 '11 at 12:59
    
@BrianFegter Can't get this to work in WP 3.3.2. Did something change? I changed restricted_users and restricted_pages to correspond to my page and user. –  INT Oct 1 '12 at 21:07
    
@INT try wp_trash_post instead. Ref: adambrown.info/p/wp_hooks/hook/trash_post –  adrian7 Feb 1 '13 at 15:14
add comment

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

share|improve this answer
    
Roman Wünsche Thanks for the answer but i want to use this role ONLY for particular pages. Is there any solutions. He is the only admin in that site –  user8917 Sep 25 '11 at 1:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.