Creating a "Reviewer" role for users

Hello,

I am currently building Hivepress website (hosted on LocalWP for now) where all listings submited by users will have to be reviewed before being published. For that, I wanted to create a Reviewer role that would have access only to review and publish the listings created by users (from the WP dashboard, just like the administrator currently does it) using this code:

function add_reviewer_role() {
    add_role('reviewer', 'Reviewer', [
        'read'                  => true,
        'edit_posts'            => false, // Cannot edit WordPress blog posts
        'delete_posts'          => false,
        'publish_posts'         => false,
        'upload_files'          => false, // 
        'edit_hp_listings'      => true, // Can edit listings
        'edit_others_hp_listings' => true, // Can edit others' listings
        'publish_hp_listings'   => true, // Can publish listings
        'delete_hp_listings'    => false, // Cannot delete listings
        'manage_hp_categories'  => false, // Cannot edit listing categories
    ]);
}
add_action('init', 'add_reviewer_role');

The addition of the new role went fine, however when I assign it to a user and then log into Wordpress as that user, I automatically get redirected to the site frontend, and I am not able to access the backend (tried accessing the wp-admin by modifying the URL, but this also redirects on frontend). I tried forcing/allowing admin view using following code:

function allow_reviewer_admin_access() {
    if (current_user_can('reviewer') && !defined('DOING_AJAX')) {
        show_admin_bar(true); // Show admin bar
    }
}
add_action('after_setup_theme', 'allow_reviewer_admin_access');
function bypass_hivepress_redirect($redirect, $user) {
    if (isset($user->roles) && in_array('reviewer', $user->roles)) {
        return admin_url(); // Force redirect to WP Admin
    }
    return $redirect;
}
add_filter('login_redirect', 'bypass_hivepress_redirect', 10, 2);

, but it still didn’t work.

Any ideas what I could do to achieve this?

Hi,

Please note that we are using capability types for posts, but we are planning more precise capabilities.

As for the redirect, the solution is to enable Restrict access to the WordPress back-end in HivePress > Settings > Users or to provide the ability to publish posts to this role using a custom implementation: hivepress/includes/components/class-admin.php at master · hivepress/hivepress · GitHub

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.