Basiclly, I must Remove the next roles whenever the admin is creating a new user and clicks on the dropdown menu tu display all the available roles:
Contributor
Editor
Unapproved User
Author
It should only display Subscriber and Administrator.
Also I must change the name of Subscriber to something different, where is this code? This problem would seem to me like there was a plugin to it.
To remove a role you can use
<?php remove_role( 'editor'); ?>
and to rename a role add the following php t your functions.php file
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Owner';
$wp_roles->role_names['administrator'] = 'Owner';
}
add_action('init', 'change_role_name');
(The above is from here
There is a function called remove_role that WordPress has in its PHP API. you use it like this:
<?php remove_role('author'); ?>
author could be replaced with any role you want. The way you want to use this command is different than most WP commands. This is because it removes the role from the DB so any future times, the role is already removed. I would recommend making a custom WP plugin that just removes the roles you want on activation.WP Plugin Help
Related
Wordpress PHP:
I am in need of a small php script that allows me to check for the userrole of the active logged in user. And to output it as a class item.
I have assigned User Roles via Ultimate Member - if that has anything to say.
Example:
A user is a member of a role, named: "storeowner".
I would like to add that to a div as a shortcode of sorts.
Is there a quick and dirty solution out there ?
Sorry if this has been asked and answered, but I haven't been able to find it.
You can create a helper function to return a space separated list of user roles "role1 role2 role3":
function my_get_role_classnames($uid) {
$user_meta = get_userdata($uid);
$user_roles = $user_meta->roles;
return implode(" ", $user_roles);
}
Then use like so:
class="<?php echo my_get_role_classnames(1); ?>"
I try to allow specific user to install and manage plugins. His role is Store Manager (WooCommerce), and this is my functions:
function add_theme_caps() {
// Get & Set user ID
$shopmanager_id = 7;
$shopmanager = new WP_User( $shopmanager_id );
// Let him manage and install plugins
$shopmanager->add_cap( 'edit_plugins', true );
$shopmanager->add_cap( 'install_plugins', true );
}
add_action( 'admin_init', 'add_theme_caps');
But it's not working. How to achieve that?
OK, so I made this in different way. There was some problem with Manager role from WooCommerce. So I decide to add this user ADMIN role, and then lower its privileges with custom code, if/else and custom redirections via functions.php.
I am trying to find a way to customize the list_users capability in Wordpress,
I have created a new role called my_admin and i added some capabilities to it.
The problem is that the list_users capability displays all users while i want to display only a group of users according to a user meta field.
Another question about the edit_users capability can i limit this capability to edit the users without promoting them, this capability enables editing the users and also promoting them which means that it enables changes to user roles also. Is there any way i can remove this promoting feature from it, can i also select only one input field to be edited and remove other fields?
function my_new_service_capabilities() {
// Get the role object.
$editor = get_role( 'my_admin' );
// A list of capabilities to add.
$addcaps = array(
'read',
'edit_posts',
'list_users', //I need to list users according to a user meta value.
'edit_users', //I need to enable editing users in only one input field
'moderate_comments',
);
foreach ( $addcaps as $acap ) {
// Add the capability.
$editor->add_cap( $acap );
}
}
add_action( 'init', 'my_new_service_capabilities' );
I think this should work for you →
https://codex.wordpress.org/Class_Reference/WP_User_Query
steps →
Retrieve all users.
User For each loop.
Use meta_query parameter, because WP stores capabilities as json
string in user_meta.
I am giving you a clue how to do this →
$entire_user_list = get_users();
$somse_users= array();
foreach($entire_user_list as $user){
if($user->has_cap('specific_capability')){
$somse_users[] = $user;
}
}
I had solved it using the DOMDocument class.
I was unable to login to wp-admin "You don't have sufficient privileges" so I added this code to my themes functions.php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
function codelight_all_permissions( $allcaps, $cap, $args ) {
$allcaps[$cap[0]] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'codelight_all_permissions', 0, 3 );
Then I was able to get in as site admin, I went to users and noticed there are no roles assigned. In the database I have level 10 and a:1:{s:13:"administrator";s:1:"1";} assigned.
If I create a new user I cannot choose a role for that user from the dropdown. I only get the option "No role for this site". Does anyone know how I can troubleshoot this issue?
I am trying to find a way to limit Drupal Taxonomy Terms that appear on Content Add form based on their Role permissions but also on their user id. I have a taxonomy set called Departments (IT, Marketing, Accounting, etc).
On this specific content add form, there is a dropdown which asks you to assign the department that the content will belong to. I would like it to be the full dropdown list if the role is set as Admin however if the it is a regular user, I want it to instead be defaulted or locked to the department that they are assigned to in their user profile.
I looked around and have found multiple suggestions online such as setting up an entity reference field and using Taxonomy Access Control module but I can't seem to set it up correctly. Any suggestions?
Thanks!
I think the easiest way to achieve this would be a little code in a custom module.
First I would use hook_permisision to create a new permission called manually_set_department.
It is preferable to check for permissions not roles as roles can change and it is more flexible once a permission is set up as you can apply it to other roles if needed.
To do this use:
function MY_MODULE_permission() {
return array(
'manually_set_department' => array(
'title' => t('Manually set department'),
'description' => t('Allows users to manually set their department'),
),
);
}
Now we need to cut down the list of departments if the user does not have the above permission. To do this we can use hook_form_alter. In this example I'm assuming your content type is called page:
function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {
drupal_set_message("Form ID is : " . $form_id);
switch($form_id) {
case 'page_node_form':
// dpm($form); // dpm (supplied by the devel module) is always useful for inspecting arrays
if(!user_access('manually_set_department')) { // check the permission we created earlier
global $user;
$account = user_load($user->uid); // load the users account
//dpm($account);
$deparment_tid = $account->field_department[LANGUAGE_NONE][0]['tid']; // get their department tid
foreach($form['field_department'][LANGUAGE_NONE]['#options'] as $k => $v) {
if($k != $deparment_tid) unset($form['field_department'][LANGUAGE_NONE]['#options'][$k]); // unset any but their own departments
}
}
break;
}
}