WordPress: User with custom role cannot access wp-admin - wordpress

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);

Related

show only custom plugin menu for custom user role user using add role hooks

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

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.

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
)
);

Limit WordPress user to edit only his own pages

I'm looking for the simplest way to limit a WordPress user to edit only his own pages (that is pages he is author of). I've read about some users manager plugins but for my needs they seems overkill and so I wonder if it is possible to obtain the same result adding some code lines to functions.php or something similar.
you can do this by adding a new role like so :
<?php add_role( $role, $display_name, $capabilities ); ?>
This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation
Returns a WP_Role object on success, null if that role already exists.
Example
Create a new "Basic Contributor" role.
$result = add_role(
'basic_contributor',
__( 'Basic Contributor' ),
array(
'read' => true, // true allows this capability
'edit_posts' => true,
'delete_posts' => false, // Use false to explicitly deny
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
}
else {
echo 'Oh... the basic_contributor role already exists.';
}
add_role() is located in wp-includes/capabilities.php.
for more clarification look in this article

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.

Resources