0

I have created a job offers plugin with two custom post types job_offer and job_application.

Now I'm trying to create a custom user role "HR Manager" with custom capabilities. This user role should only be able to manage (add, edit, delete..) job offers and job applications.

So i created the role and added some capabilities (code below):

function add_roles_on_plugin_activation() {

    $role = add_role(
        'hr_manager',
        __('Responsable RH', 'abc_job_offers'),
        array(
            'read' => true
        )
    );

    $role->add_cap('edit_job_offer');
    $role->add_cap('edit_job_offers');
    $role->add_cap('edit_other_job_offers');
    $role->add_cap('publish_job_offers');
    $role->add_cap('read_job_offer');
    $role->add_cap('read_private_job_offers');
    $role->add_cap('delete_job_offer');

    $role->add_cap('edit_job_application');
    $role->add_cap('edit_job_applications');
    $role->add_cap('edit_other_job_applications');
    $role->add_cap('publish_job_applications');
    $role->add_cap('read_job_application');
    $role->add_cap('read_private_job_applications');
    $role->add_cap('delete_job_application');

}

register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );

And i changed my CPT registration so that the capabilities become the hr_manager role capabilities like this:

$args = array(
        "label" => __( "Offres d'emploi", "abc_job_offers" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "capabilities" => array(
            'edit_post' => 'edit_job_offer',
            'edit_posts' => 'edit_job_offers',
            'edit_others_posts' => 'edit_other_job_offers',
            'publish_posts' => 'publish_job_offers',
            'read_post' => 'read_job_offer',
            'read_private_posts' => 'read_private_job_offers',
            'delete_post' => 'delete_job_offer'
        ),
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "job-offer", "with_front" => true ),
        "query_var" => true,
        "menu_icon" => "dashicons-businessman",
        "supports" => array( "title", "editor", "thumbnail" ),
    );

    register_post_type( "job_offer", $args );

But when i log in with a user with HR manager role i can't see the CPT menu on admin dashboard, i only see dashboard and profile, i don't know what i'm missing.

  • 1
    I figured out what was wrong, if anyone is interested: *"capability_type" should be the name of the CPT. *"capabilities" won't be needed then. the code i added in the registration hook is supposed to be called once, so when you activate then deactivate and then reactivate the plugin, the user role doesn't change it keeps it's capabilities given first time, so a workaround is the call remove_role( 'hr_manager' ); right before $role = add_role( 'hr_manager', __('Responsable RH', 's2m_job_offers'), array( 'read' => true, ) ); or in the deactivation hook. – Badr Feb 26 at 15:24

Your Answer

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

Browse other questions tagged or ask your own question.