While WordPress can be used as a personal blogging platform with just one user, most large sites have multiple authors writing about different things under various categories. WordPress has a sophisticated system of roles and permissions that allow fine tuning of what each user can do. By default however, an editor can go to “Posts->All Posts” in the WordPress dashboard and modify any existing blog entry – regardless of whether they wrote it or not. This requires a certain amount of trust to be placed in your editors.

If you have a WordPress site with a lot of editors, you might want to ensure that they can only edit their own posts – and not those belonging to someone else. Moreover, you might not even want them to be able to see other’s posts. In this article, I’ll show you how to achieve this and allow complete separation between your various editors.

First Step: The “Contributor” Role

The most obvious solution to prevent a user from being able to modify posts written by someone else, is to make them a “Contributor” instead of an “Editor”. This can be done right from the user creation screen itself. When adding a new user, simply choose “Contributor” from the drop down list as shown here:

After your user logs in for the first time, you can see in the screenshot that they’re only able to view the posts of others. They can’t actually edit or change them since the links are missing:

But if it was only a question of links, then what about direct URLs that point to the edit page of a particular post? Don’t worry – it turns out that even accessing an edit URL isn’t allowed. If the user attempts to directly access an edit page for a post that doesn’t belong to them, they will receive an error message telling them that they’re not allowed:

So the simplest solution to prevent people from modifying posts written by others, is to make them Contributors instead of users. If your users have already been created, you can modify their roles by going to “Users->All Users”, selecting the users, and changing their roles from the drop down box as shown here:

Preventing Contributors from Viewing Other People’s Posts

As you can see above, Contributors can only modify their own posts. However, they can still see what others have written. They can see the total numbers of posts on your blog, how many are pending, how many drafts, number published etc etc. There will be times when you just want to hide all this information entirely. Unfortunately, there isn’t any direct option in WordPress that will allow this, and we have to write our custom code to make it happen.

Add the following code into your functions.php file, or wherever you place custom PHP code:

function alter_the_edit_screen_query( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
        if ( !current_user_can( 'activate_plugins' ) )  {
add_action( 'views_edit-post', 'remove_items_from_edit' );
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}

add_filter('parse_query', 'alter_the_edit_screen_query' );

function remove_items_from_edit( $views ) {
    unset($views['all']);
    unset($views['publish']);
    unset($views['trash']);
    unset($views['draft']);
    unset($views['pending']);
    return $views;
}

After saving the changes, the user with the Contributor role will no longer be able to see any of the other posts regardless of their status. Here’s the edit page after the above code has been added:

Allow WordPress users to edit only their own posts

Since this is a new user I just created, there are no existing posts. But everything else has vanished. With this, you have achieved a complete separation – a wall between users that they can’t penetrate. They will have no idea what others have written, or how much.

So even though there’s no direct way in WordPress to prevent users from seeing and editing each other’s posts, we can make it happen with a bit of clever coding!

Is your website slow?

Enter its URL below to find out now:

About the Author

Bhagwad Park

Comments

  • Awesome post Bhagwad! This is very helpful. Thanks for sharing!