Skip to content
Advertisement

WordPress – Second Custom Role Not working

I have successfully created a custom user role called Client and I am using the same structure to create a second custom user role called Broker.

I have been stumped here all morning for something so simple, there is no difference other than the required variables and names.

The second user role does not show up in the list when I add new role in the WP Admin back-end.

Any thoughts?

Edit: There are two custom post types being given permissions here, for reference they are an Incident and Document custom post-type.

  
/*
//
// Client User Role
//
*/

//* Register activation hook to add Client role
register_activation_hook( __FILE__ , 'addClient' );

//* Register deactivation hook to remove Client role
register_deactivation_hook( __FILE__ , 'removeClient' );

function addClient() {
  $capsClient = [
    //* Meta capabilities
    'read'                   => false,
    'edit_'              => false,
    'read_'              => false,
    'delete_'            => false,

    //* Primitive capabilities used outside of map_meta_cap()
    //incidents
    'edit_incidents'             => true,
    'edit_others_incidents'      => false,
    'publish_incidents'          => true,
    'read_private_incidents'     => false,
    //documents
    'edit_documents'             => true,
    'edit_others_documents'      => false,
    'publish_documents'          => true,
    'read_private_documents'     => false,

    //* Primitive capabilities used within of map_meta_cap()
    //incidents
    'delete_incidents'           => true,
    'delete_private_incidents'   => true,
    'delete_published_incidents' => true,
    'delete_others_incidents'    => false,
    'edit_private_incidents'     => false,
    'edit_published_incidents'   => true,
    //documents
    'delete_document'           => true,
    'delete_private_documents'   => true,
    'delete_published_documents' => true,
    'delete_others_documents'    => false,
    'edit_private_documents'     => false,
    'edit_published_documents'   => true,
  ];

  add_role( 'client', 'Client', $capsClient );
}

function removeClient() {
  remove_role( 'client' );
}

/*
//
// Broker User Role
//
*/

//* Register activation hook to add Broker role
register_activation_hook( __FILE__ , 'addBroker' );

//* Register deactivation hook to remove Broker role
register_deactivation_hook( __FILE__ , 'removeBroker' );

function addBroker() {
  $capsBroker = [
    //* Meta capabilities
    'read'                   => false,
    'edit_'              => false,
    'read_'              => false,
    'delete_'            => false,

    //* Primitive capabilities used outside of map_meta_cap()

    //incidents
    'edit_incidents'             => true,
    'edit_others_incidents'      => false,
    'publish_incidents'          => true,
    'read_private_incidents'     => false,
    //documents
    'edit_documents'             => true,
    'edit_others_documents'      => false,
    'publish_documents'          => true,
    'read_private_documents'     => false,

    //* Primitive capabilities used within of map_meta_cap()

    //incidents
    'delete_incidents'           => true,
    'delete_private_incidents'   => true,
    'delete_published_incidents' => true,
    'delete_others_incidents'    => false,
    'edit_private_incidents'     => false,
    'edit_published_incidents'   => true,
    //documents
    'delete_document'           => true,
    'delete_private_documents'   => true,
    'delete_published_documents' => true,
    'delete_others_documents'    => false,
    'edit_private_documents'     => false,
    'edit_published_documents'   => true,
  ];

  add_role( 'broker', 'Broker', $capsBroker );
}

function removeBroker() {
  remove_role( 'broker' );
}

Advertisement

Answer

It looks like the multiple hooks were causing an issue, I simplified the code as shown below and everything works fine now:

/*
// Client User Role
*/


$client_role = add_role('Client', __('Client'), array(
    'read'=>'true',
    'list_users'=>'true',
    'edit_users'=>'true',
    'create_users'=>'true',
    'delete_users'=>'true',

    //incidents
    'edit_incidents'             => true,
    'edit_others_incidents'      => false,
    'publish_incidents'          => true,
    'read_private_incidents'     => false,
    //documents
    'edit_documents'             => true,
    'edit_others_documents'      => false,
    'publish_documents'          => true,
    'read_private_documents'     => false,

    //incidents
    'delete_incidents'           => true,
    'delete_private_incidents'   => true,
    'delete_published_incidents' => true,
    'delete_others_incidents'    => false,
    'edit_private_incidents'     => false,
    'edit_published_incidents'   => true,
    //documents
    'delete_document'           => true,
    'delete_private_documents'   => true,
    'delete_published_documents' => true,
    'delete_others_documents'    => false,
    'edit_private_documents'     => false,
    'edit_published_documents'   => true,
    
    ));

/*
// Broker User Role
*/

$broker_role = add_role('Broker', __('Broker'), array(
    'read'=>'true',
    'list_users'=>'true',
    'edit_users'=>'true',
    'create_users'=>'true',
    'delete_users'=>'true',

    //incidents
    'edit_incidents'             => true,
    'edit_others_incidents'      => false,
    'publish_incidents'          => true,
    'read_private_incidents'     => false,
    //documents
    'edit_documents'             => true,
    'edit_others_documents'      => false,
    'publish_documents'          => true,
    'read_private_documents'     => false,

    //incidents
    'delete_incidents'           => true,
    'delete_private_incidents'   => true,
    'delete_published_incidents' => true,
    'delete_others_incidents'    => false,
    'edit_private_incidents'     => false,
    'edit_published_incidents'   => true,
    //documents
    'delete_document'           => true,
    'delete_private_documents'   => true,
    'delete_published_documents' => true,
    'delete_others_documents'    => false,
    'edit_private_documents'     => false,
    'edit_published_documents'   => true,

    ));
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement