Thoroughly impressed with and grateful for your plugin. Thank you.
We have a situation where we want to sync between sites when we add a role, e.g. using:
$user->add_role( 'my_role' );
Our initial testing indicates that this is not supported with your plugin out of the box, i.e. the user is getting the new secondary user role on the site where we do this just fine, but our second site doesn’t sync added user role. (Whereas it seems to just fine for changes made through code to the primary user role and for all changes made on the backend through https://educatorspro.com/wp-admin/user-edit.php)
Is that right?
And, if so, could you guide us on how to go about it?
Would something like this work to force a profile_update and your Wprus_Api_Update syncing along with it?
add_action( 'add_user_role', 'force_profile_update_upon_user_add_role', 20, 2 );
function force_profile_update_upon_user_add_role( $user_id, $role ) {
if( $role == 'my_role' ) do_action('profile_update', $user_id, get_userdata( $user_id ) );
}
Or, would it be better to try and extend (one of) your class(es) in order to have it fire on WP_User::add_role? e.g.:
public function init_notification_hooks() {
add_action( 'add_user_role', array( $this, 'notify_remote' ), PHP_INT_MAX, 2 );
}
The former seemed less attractive because of the possibly increased load each time a role is added, but the latter seemed less attractive because of wanting to avoid having to maintain any code that’s different from your core code.
All that said, we are running a custom plugin and can easily make changes there.
Thank you very much for your guidance.
Also, I noticed your comment about offering support only for general enquiries and bug fixes. Don’t hesitate to advise if this doesn’t fall into that. Want to respect your terms and time. And happy to engage as you wish.
Alex.
]]>Many thanks for your plugin that I have been using for a few years.
I would like to translate my custom roles created with CME as WordPress does for its default roles (ex: subscriber = “abonné” in French).
Unfortunately, I cannot find the role names in the string translation tool of the WPML plugin. Maybe you do not wrap role names with the GetText function when creating them?
Thank you in advance for your help and have a nice evening.
Kind regards,
Sylvain
The cpt does not have a public facing side.
Also only want to show the CPTs to the publisher and administrator.
So basically what I have so far in it’s current state:
However with this configuration merchants are able to see others posts and the administrator can not see the cpt.
$post_type = register_post_type('credit_app',
array(
'labels' => array(
'name' => __( 'Credit Application', $this->plugin_name ),
'singular_name' => __( 'Credit Application', $this->plugin_name ),
'menu_name' => __( 'Credit Applications', $this->plugin_name ),
'name_admin_bar' => __( 'Credit Applications', $this->plugin_name ),
'add_new' => __( 'Credit Application', $this->plugin_name ),
'add_new_item' => __( 'Credit Application', $this->plugin_name ),
'new_item' => __( 'Credit Application', $this->plugin_name ),
'not_found' => __( 'Application Not Found', $this->plugin_name ),
'all_items' => __( 'All Credit Applications', $this->plugin_name ),
),
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'has_archive' => false,
'supports' => array( 'title', 'editor', 'author', 'custom-fields' ),
'rewrite' => false,
'capability_type' => 'credit_app',
'map_meta_cap' => true
)
);
$result = add_role('ebf_merchant', __('Merchant'),
array(
'read' => true,
'credit_app' => true,
'edit_credit_apps' => true,
'publish_credit_apps' => true
)
);
$admin_role = get_role( 'administrator' );
$admin_role->add_cap( 'credit_app' );
$admin_role->add_cap( 'edit_others_credit_apps' );
Also receiving some notices and warnings:
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'capability_type' of non-object in //wp-includes/post.php on line 1261
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'capability_type' of non-object in //wp-includes/post.php on line 1262
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'capability_type' of non-object in //wp-includes/post.php on line 1262
[16-Mar-2018 13:55:53 UTC] PHP Warning: Attempt to assign property 'capability_type' of non-object in //wp-includes/post.php on line 1262
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'capability_type' of non-object in //wp-includes/post.php on line 1265
[16-Mar-2018 13:55:53 UTC] PHP Notice: //wp-includes/post.php on line 1280
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'capabilities' of non-object in //wp-includes/post.php on line 1293
[16-Mar-2018 13:55:53 UTC] PHP Warning: array_merge(): Argument #2 is not an array in //wp-includes/post.php on line 1293
[16-Mar-2018 13:55:53 UTC] PHP Notice: Trying to get property 'map_meta_cap' of non-object in //wp-includes/post.php on line 1300
]]>add_cap() function has the database storage call:
public function add_cap( $role, $cap, $grant = true ) {
if ( ! isset( $this->roles[$role] ) )
return;
$this->roles[$role]['capabilities'][$cap] = $grant;
if ( $this->use_db )
update_option( $this->role_key, $this->roles );
}
But what if I want to add new capability when creating new role. I.e. the new role should be ‘grandmaster‘ and I want to give him a new ‘can_play_chess‘ capability immediately in plugin activation:
$result = add_role(
'grandmaster',
__( 'Grandmaster' ),
array(
'read' => true, // true allows this capability
'can_play_chess' => true,
)
);
Is this is allowed, and is this is a correct way to adding both – new role and new capability.
]]>Everything is ok, but custom roles are not translated in the wordpress back-office (in user profil for example, or in settings=>general=>default role).
My plugin code is translation ready: i have created a .pot file and .po and .mo myplugin-fr_FR local. Translation files are ok, because i can see translated description and plugin name in admin plugin page.
/*
Plugin Name: My Website User Roles
Description: Custom user roles and capabilities for my website.
Version: 1.0
Author: me
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my_website_user_roles
Domain Path: /languages
*/
class My_Website_User_Roles {
public function __construct() {
// on activation
register_activation_hook( __FILE__, array( 'My_Website_User_Roles', 'activate' ) );
/** other stuff like deactivation, etc... */
// translation
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
}
public function load_textdomain() {
load_plugin_textdomain( 'my_website_user_roles', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
public static function activate() {
// remove wp default roles except admin
remove_role( 'editor' );
remove_role( 'author' );
remove_role( 'contributor' );
remove_role( 'subscriber' );
add_role(
'basic_contributor',
__( 'Occasional Contributor', 'my_website_user_roles' ),
array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
)
);
add_role(
'expert_contributor',
__( 'Expert Contributor', 'my_website_user_roles' ),
array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'publish_posts' => false,
'upload_files' => true,
'edit_others_posts' => true,
'edit_published_posts' => true,
)
);
add_role(
'moderator',
__( 'Moderator', 'my_website_user_roles' ),
array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
'publish_posts' => true,
'edit_published_posts' => true,
'delete_published_posts' => true,
'moderate_comments' => true,
)
);
}
}
On my languages directory, i have created my_website_user_roles-fr_FR.po and my_website_user_roles-fr_FR.mo files that should translate Occasional Contributor to ‘contributeur occasionnel’, Expert Contributor to ‘contributeur expert’ and Moderator to ‘modérateur’.
But in the wordpress admin back office, custom user roles are in english but not in french.
Localisation files are loaded because they also translate the plugin description and i can see the description in french on the plugins page.
How can i see the custom user roles in a local language on the back office ? what am i doing wrong ?
]]>In my project I’ve defined some new roles using add_role() so that I can display content to users based on their custom roles. Some roles are identical in their capabilities… it’s only the content that they can access that differentiates between them.
I should mention that I’m using the Advanced Custom Fields plugin to create dynamic content fields on custom post types using the Repeater add-on.
The project runs on a WordPress Network installation.
I have two questions:
1. What is the simplest way of getting at the current user’s role?
I found this thread (which is 4 years old): Get a users role by user id which resulted in my current code, which works but relies on users only having one role, and WordPress seems to store roles in an array (I don’t know why).
$current_user = wp_get_current_user();
if ( !empty( $current_user->roles ) && is_array( $current_user->roles ) ) {
foreach ( $current_user->roles as $role ) {
$current_user_role = $role;
}
if ($current_user_role == 'administrator') {
$user_level = 4;
} elseif ($current_user_role == 'custom_level_3') {
$user_level = 3;
} elseif ($current_user_role == 'custom_level_2') {
$user_level = 2;
} elseif ($current_user_role == 'custom_level_1') {
$user_level = 1;
}
} else {
$current_user_role = 'visitor';
$user_level = 0;
}
As you can see, I want to convert the user’s role into an integer so that I can compare to the access level (also an integer) for each section of content.
I am concerned it would break if a user had more than one role for some reason that I don’t understand…
2. What is the best way or removing the default WordPress roles?
In this project, the default roles will be redundant. I am working within functions.php at the moment, although I will probably abstract that user role functionality into a plugin…
Anyway, if I was to use remove_role(), would the default WordPress roles be available when I switch themes or deactivate the plugin?
Thanks,
Andy
]]>When a user registers, I am first setting their role to Editor, but once my plugin activates, I need to change their role to Administrator.
Below is my current code:
function ap_change_role() {
global $wpdb;
$blog_id = get_current_blog_id();
$query = $wpdb->prepare("
SELECT 'user_id'
FROM '%susermeta'
WHERE ( meta_key LIKE 'primary_blog'
AND meta_value LIKE %d )
LIMIT 1", $wpdb->base_prefix, $blog_id);
$blog_owner_id = $wpdb->get_var( $query );
$user = new WP_User( $blog_owner_id );
// Remove role
$user->remove_role( 'editor' );
// Add role
$user->add_role( 'administrator' );
}
register_activation_hook( __FILE__, 'ap_change_role');
But the site owners role is not changing after activation.
Any help with getting this to work will be much appreciated.
Thanks
]]>I have successfully added a new role via a php function that I added in functions.php. My problem is when you go to users list : /wp-admin/users.php
Role column, and the new role is not the same. For example on this image:
https://goo.gl/2iOIB
This is what it should be. The Role Column (with red circle) and the New role (with yellow highlight) should be the same. Using the add_role() function only adds the New Role while the Role Column stays the same as the default, which is “Subscriber“.
I’m not sure what you call that name in the Role Column but any of you know what I need to add in my function so it would also change? I need it to be the same coz if it’s not, simplepress forum does not recognize the new role.
Thanks in advance!
]]>Ex: Sellers (A user level where they can see the sales progress, donwload .pdf, etc.)
I went in my function.php file and I wrote this down.
add_role( 'sellers', 'Seller', array(
'read' => true, // True allows that capability
'publish_pages' => true,
'read_private_pages' => true,
'publish_posts' => true,
'edit_posts' => true,
'read_private_posts' => true
));
It actually works well for the new user.. But when I try to see the “private posts or things like that”, I see nothing..
]]>remove_role('test');
add_role('test', 'Test', array(
'edit_theme_options' => true,
'moderate_comments' => true,
'manage_categories' => true,
'unfiltered_html' => true,
'edit_others_posts' => true,
'edit_pages' => true,
'edit_others_pages' => true,
'edit_published_pages' => true,
'publish_pages' => true,
'delete_pages' => true,
'delete_others_pages' => true,
'delete_published_pages' => true,
'delete_others_posts' => true,
'delete_private_posts' => true,
'edit_private_posts' => true,
'read_private_posts' => true,
'delete_private_pages' => true,
'edit_private_pages' => true,
'read_private_pages' => true,
'edit_published_posts' => true,
'upload_files' => true,
'publish_posts' => true,
'delete_published_posts' => true,
'edit_posts' => true,
'delete_posts' => true,
'edit_theme_options' => true,
'read' => true
));
But a “TEST” type user have in it’s profile this list of denied powers:
Denied: NextGEN Change options, Denied: NextGEN Change style, Denied: activate_plugins, Denied: add_users, Denied: create_users, Denied: delete_plugins, Denied: delete_themes, Denied: delete_users, Denied: edit_dashboard, Denied: edit_plugins, Denied: edit_theme_options, Denied: edit_themes, Denied: edit_users, Denied: export, Denied: import, Denied: install_themes, Denied: list_users, Denied: manage_links, Denied: manage_options, Denied: promote_users, Denied: remove_users, Denied: switch_themes, Denied: update_core, Denied: update_plugins, Denied: update_themes, Denied: install_plugins, ngg_general, tinymce, add_gallery, ngg_manage_gallery, manage_others, manage_tags, edit_album, change_style, change_options, general
So, also denied “edit_theme_options”.
Can you help me understanding why ?
]]>