Is it possible to add multi role in wordpress? - wordpress

I am building an education site, in that I want to add Teacher and Student as different user with some capabilities. I tried many times, but only default role is working. How can I assign different role for student and teacher....?
Tried many plugin also... At the time of new user registration, the role can select.
I tried these also
/* 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');
Is it possibile to add multi role in wordpresss?
How can hide the default role?
please help me..
Thanks...

Yes it is possible to create multi-role in WordPress. You can use User Role Editor for creating different roles with different capabilities.

Related

Add alternate customer role to WordPress / WooCommerce

I am trying to add another customer role to WordPress and WooCommerce. I will use this new customer role to assign alternate prices when the user is logged in. My code works but I cannot find what permissions a customer has in WordPress / WooCommerce by default. I want this new role to have identical permissions to the default customer account. The code below is located in my child functions.php file.
/* Custom user roles */
add_role('distributor', __(
'Distributor'),
array(
'read' => true, // Allows a user to read
'create_posts' => true, // Allows user to create new posts
'edit_posts' => true, // Allows user to edit their own posts
'edit_others_posts' => true, // Allows user to edit others posts too
'publish_posts' => true, // Allows the user to publish posts
'manage_categories' => true, // Allows user to manage post categories
)
);
You can use the capabilies of another role and use this as the "capabilities array" when you create the new user role with the add_role() function of wordpress. I assume the role you want the capabilities to be copied over is called customer. You can adjust this.
add_role( 'distributor', 'Distributor', get_role( 'customer' )->capabilities );
The function accepts an array of capabilites. With get_role() you get an role object and access the capabilities of that role.
So we create a new role with the capabilities of an existing role.

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 add roles capabilities edit user

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.

Add new roles into the role_hierarchy automatically

I made a form for an administrator to add users and give them roles, so I added this line in the RegistrationFormType
//....
->add('roles', 'choice', array(
'choices' => array(
'ROLE_ADMIN' => 'ROLE_ADMIN',
'ROLE_USER' => 'ROLE_USER',
'ROLE_AUTEUR' => 'ROLE_AUTEUR',
'ROLE_MODERATEUR' => 'ROLE_MODERATEUR',
),
'required' => false,
'empty_value' => 'Choisissez un ou plusieurs roles',
'empty_data' => null,
'multiple' => true,
'expanded' => false,
))
//.....
The code works and the roles are added in the database.
My question is, if in the future, an administrator wants to add other new roles, so how to can add them into the role_hierarchy automatically?
I think this make not really sense then when you add a new Role this has no effect if you don't use the role in your application. So you should define the role structure before and make it so granular you need.
But you don't need to define the roles in your FormType. You can implement that the roles are loaded from an database. So you can manage your roles in your database and define the role hierarchy in your configuration file.
http://symfony.com/doc/current/cookbook/security/entity_provider.html#managing-roles-in-the-database
Here is the section in the documentation to manage roles from the database. I think this could help you.
Edit:
I have found something for you
http://symfony.com/doc/current/components/security/authorization.html
You can define voters in your case you could try a RoleHierarchyVoter or RoleVoter but thats a lot of work to implement it.

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