7

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.

10

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

  • 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
5

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);
  • 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
  • 1
    @INT try wp_trash_post instead. Ref: adambrown.info/p/wp_hooks/hook/trash_post – adrian7 Feb 1 '13 at 15:14
0

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.');
    }
}
  • 1
    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
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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