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.
Related
I have created a custom user role for new users in functions.php like below codes:
add_action( 'admin_init', 'add_custom_roles' );
function add_custom_roles() {
$student = add_role( 'student', 'Student', array(
'read' => false,
'assignments' => true,
));
if( null !== $student ) {
$role->add_cap( 'assignments', true);
}
}
Want to allow access student roles only to view my custom plugin page: assignments but it is not working. Custom plugin is created by add_menu_page hook.
You are getting close to your goal, to help yourself you could install this (https://it.wordpress.org/plugins/members/) which make capabilities handling easier, and then tweak the settings in order to obtain the menĂ¹ visibility you want.
You should "clone" the minimum WP user role required to enter the backend (should be contributor if i remember good) and then add your custom permission to access plugin page
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);
I'm using the pie registration plugin and I want to know how can I automatically define a role (in my case I want to define as subscriber) when people register and login. I want to do this because I want to restrict some zones of the website and redirect those zones for the register or login page. Since the free version of the plugin doesn't have restricted areas, I'm also using the plugin called members that does 50% of what I want. So I want to combine these 2 plugins' functionalities.
tldr: how can I give the role subscriber on pie-registration?
When you are going to register user you have assign it a role for ex.
if you are using wp_insert_user( $userdata ) function than you can assign user role in $userdata as follows.
$userdata = array(
'user_login' => 'login_name',
'user_pass' => 'pass',
'role' => 'role' // When creating an user, `user_pass` is expected.
);
There are some default roles in wordpress , you can also creates new roles by using following code.
<?php add_role( $role, $display_name, $capabilities ); ?>
And you can assign capabilities, so by using this two function you can achieve your goal.
You can read more about:wp_insert_user , add_role
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.
In registration, after user registered, they should confirm the email validation link, until they validate the email their role should be inactive and they should not able to login in to the website.
Am adding the user using wp_insert_user(), in this function there is a field name call 'role' but I don't know what should be given there. I tried "inactive" but not working. Kindly suggest me.
Assuming your new users are assigned the 'subscriber' role on registration, you should hook to the 'user activated' hook and do something like:
wp_update_user( array ( 'ID' => $user_id, 'role' => 'author' ) ) ;
In your application you should ensure the 'subscriber' is locked-down or inactive (i.e. can't do anything much) and your 'author' role has the full set of capabilities that you need your active users to have.