WordPress add roles capabilities edit user - wordpress

I want to add the capability of adding/eddeting/removing users to a new role.
However I can't find the right capabilities.
Any help please?
Current (from the WordPress Codex)
add_role('new-role', 'New Role', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
));
I would like to add something like: add_user, edit_user and remove_user
M.
-EDIT--EDIT--EDIT--EDIT--EDIT--EDIT-
I have made some progress.
This is what I have now:
add_role('new-role', 'New Role', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
'edit_users'=> true,
'level_10'=> true,
'delete_users' => true,
'create_users' => true,
'list_users'=>true,
'remove_users' > true,
'add_users' => true,
'promote_users'=> true
));
I can now add users but I can't edit users. The "user" tab doesn't show.
There is a "add new user" button under the "profile" menu.
M.
-EDIT--EDIT--EDIT--EDIT--EDIT--EDIT-
Okay I fixed it.
I was allready using the 'Adminimize' plugin so I duplicated the "Administrator"-role and hide everything that I didn't need:
function cloneRole()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('administrator');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('new_role', 'My Custom Role', $adm->capabilities);
}

Sadly, there is no ability within WordPress to set a role to be able to edit users. Reading through the WordPress source code, there is only the add and delete options for users but there are edit buttons for plugins, themes, posts, pages etc.
The actual code can be found here: WordPress Capabilities Trac
The lines it shows the user related roles are around line 1273 to 1288.
Sorry to disappoint.

Related

How to display/allow 'add to cart' for private products in Woocommerce

I'm trying to prepare b2b woocommerce with custom functions only for registred users. For two days I've been stuck over function that allow add to cart private products. Do any of you have a function that will make it possible?
My capabilities for custom user:
array( 'read_private_posts' => true, 'read_private_products' => true
If someone is still looking for an answer, I have found a solution. It's not perfect because you need to work on additional security. In my case, it helped a lot. Custom users need this capabilities:
'read_private_pages' => true,
'read_private_posts' => true,
'read_private_products' => true,
'edit_others_products' => true,
'edit_private_products' => true

How to hide Add New custom post type button

I want to hide admin side my custom type Add New button. Review this screen shot -> https://s.nimbus.everhelper.me/share/1406976/p6f5u0nlvs5xff3sr85h
So any one know solutions then please update me.
Thanks.
you can use meta capability create_posts that is not documented but is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
Where custom_post_type_name you can put your custom post type name, which you are creating.

WordPress: User with custom role cannot access wp-admin

First of all I am a WordPress learner. So sorry if my code looks stupid!
I have created a custom theme with a custom user role. I am not developing any plugin.
In my fucntions.php file I have written the following code to create a User role. Users assigned to this role are supposed to login to the admin but only be able to access their Profile pages.
add_action('init', 'yrc_cst_register_role_customer_service_rep');
/**
* Register new user role
*/
function yrc_cst_register_role_customer_service_rep() {
$wp_roles = new WP_Roles();
$wp_roles->remove_role('subscriber');
$wp_roles->remove_role('editor');
$wp_roles->remove_role('contributor');
$wp_roles->remove_role('author');
$service_rep_caps = array(
'read' => false,
'create_posts' => false,
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'manage_categories' => false,
'manage_options' => false,
);
add_role('customer_service', __('Customer Service'), $service_rep_caps);
}
I have removed all roles except Administrator, because no other role is required for this portal. Administrator will only create Users with Customer Service role.
I have no third party plugin installed in the system.
Users with the custom role are able to login to the system through a custom login page which is working OK. But whenever they are trying to access their Profile page the following error message comes up:
Sorry, you are not allowed to access this page.
Is there anything like 'edit_profile' => true?
I must be doing something wrong but my limited knowledge is not enough to figure this out. Any suggestion would be highly appreciated.
You might be able to do it like this :
This should clone the subscriber role capabilities and create your role for it.
add_action('init', 'CreatecloneRoleSubscriber');
function CreatecloneRoleSubscriber()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$sub = $wp_roles->get_role('Subscriber');
//Adding a 'new_role' with all subscriber caps
$wp_roles->add_role('customer_service', 'Customer Service', $sub->capabilities);
}
EDIT : Read discussion in question comments
Just change the manage_options to true in your case
But note that by allowing manage_options to true, those user will have access to other parts of dashboard as well
$service_rep_caps = array(
'read' => false,
'create_posts' => false,
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'manage_categories' => false,
'manage_options' => true, // Most plugins and pages check for manage_options for checking access level to allow access to pages and settings.
);
Just found this solution which i consider really clean to enter wp-admin with a new role:
Add access cap to backend
Add the cap view_admin_dashboard & read to your new role.
Show the admin bar
Add this function to your wordpress.
# functions.php
add_filter( 'show_admin_bar', function () {
if ( current_user_can( 'view_admin_dashboard' ) )
return true;
return false;
}, 10);

Add Role to Registration in WP

I am looking for a way to add a dropdown for users in the registration form to select their role (except administrator).
Currently running wordpress 4.7.4 with buddypress 2.8.0., found some snippets and such but none of them are actually working.
Any help is much appreciated
You have to create user-role.php fileand include it in functions.php file.
<?php
if( get_role('subscriber') ){
remove_role( 'subscriber' );
}
if( get_role('client') ){
remove_role( 'client' );
}
// Add a Country (Others) role
$result = add_role( 'country', __(
'Country (Others)' ),
array(
'read' => true, // true allows this capability
'edit_posts' => true, // Allows user to edit their own posts
'edit_pages' => true, // Allows user to edit pages
'edit_others_posts' => true, // Allows user to edit others posts not just their own
'create_posts' => true, // Allows user to create new posts
'manage_categories' => true, // Allows user to manage post categories
'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
);

Wordpress add custom roles as well as remove default roles

I need to customize the default roles as I need only 3 roles - administrator, buyer, seller.
Then I need to add the buyer, seller and remove all other default roles. What should I do?
If there is any ready made code which I can paste in and it will work?
Paste this code in your themes function.php file and customize as your need. This is from my own code library. So it will definitely work.
/* Add member role to the site */
add_role('member', 'Member', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* Add snypher role to the site */
add_role('snypher', 'Snypher', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* remove the unnecessary roles */
remove_role('subscriber');
remove_role('editor');
remove_role('author');
remove_role('contributor');

Resources