Giving editor access to specific posts in WordPress

We know that by default WordPress has 6 user roles. And we can also create a lot of custom user roles. But sometimes we may need quite different ability. For example i want editor users to be able to edit only specific pages, not all pages. Contributor user role is not solution (contributor can edit only his own created posts, but in our case it doesn’t matter who has created posts,pages. And more than one user should be able to edit specific post)

wordpress-user

So let’s go to code. Open your theme’s functions.php and paste this code there:


if (is_user_logged_in()) {

  global $wpdb;
  $curuser=wp_get_current_user();
  $role = $wpdb->prefix . 'capabilities';
  $current_user->role = array_keys($curuser->$role);
  $role = $curuser->role[0];
  if ($role=='editor' and !empty($_GET["post"]) and $_GET["post"]!='666') { //change 666 to your specific post ID
      die ('No permission');
    }

}

That’s all. After apply your editor users will be able to edit only post with ID 666.
Or you can do vice verca. You can forbid editors to edit specific public pages/posts


if (is_user_logged_in()) {

  global $wpdb;
  $curuser=wp_get_current_user();
  $role = $wpdb->prefix . 'capabilities';
  $current_user->role = array_keys($curuser->$role);
  $role = $curuser->role[0];
  if ($role=='editor' and !empty($_GET["post"]) and $_GET["post"]=='666') { //change 666 to your specific post ID
      die ('No permission');
    }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.