Add secondary role to the new registered user

User Role Editor allows to assign multiple roles per user. What if you need that new registered users get multiple roles by default just after their registration?
For the time being you may do it with this piece of code included into your active theme functions.php file:

add_action( 'user_register', 'add_secondary_role', 10, 1 );

function add_secondary_role( $user_id ) {
    
    $user = get_user_by('id', $user_id);
    $user->add_role('screen_reader');

}

The code above adds to every new registered user the secondary role ‘screen_reader’ in addition to the default role ‘subscriber’ added by WordPress itself.

It works, but what will be when you switch to the other theme? Yes, it will stop working until you will not duplicate the code at new active theme functions.php file. If you change blog theme periodically or if you work with multi-site WordPress installation you may need more universal decision. With “multi-site” in mind we should use modified code:

if (is_multisite()) {
    add_action( 'wpmu_activate_user', 'add_secondary_role', 10, 1 );
} else {
    add_action( 'user_register', 'add_secondary_role', 10, 1 );
}
 
function add_secondary_role( $user_id ) {
 
    $user = get_user_by('id', $user_id);
    $user->add_role('screen_reader');
 
}

It works universally now as for single-site, as for multi-site WordPress installations. In order to not insert this code into every theme from themes poole which your subsites use, we may apply it another way.

WordPress has a convenient feature called “must-use” plugins. How does it work?

Create folder wp-content/mu-plugins, place there file with ‘.php’ extension and some PHP code inside. That’s it. This code will be executed by WordPress for every page request. There is no need to activate it. And it can not be deactivated with WordPress administrator interface.
So, create PHP file, for example must-use.php, insert code you see below into that file:

if (is_multisite()) {
    add_action( 'wpmu_activate_user', 'add_secondary_role', 10, 1 );
} else {
    add_action( 'user_register', 'add_secondary_role', 10, 1 );
}
 
function add_secondary_role( $user_id ) {
 
    $user = get_user_by('id', $user_id);
    $user->add_role('screen_reader');
 
}

Do not forget to add he required ‘<?php’ tag at the 1st line. Put must-use.php file into wp-content/mu-plugins folder. You did it.

P.S. I included this feature (multiple default roles) to my development plan. So it will appear as an option at the “User Role Editor Pro” soon.

Unlock Smart Manager for WooCommerce Shop Manager

In this post we will talk about how to unlock free version of Smart Manager for WooCommerce Shop Manager user.

If you ever used Smart Manager for e-Commerce WordPress plugin with WooCommerce e-shop, you may asked the question:
– Why WooCommerce super-user with ShopManager role does not see “Smart Manager” menu item under the “Products” menu?

For some reasons the free version of “Smart Manager” is available for the user with “Administrator” role only. But Pro version of “Smart Manager” has the user interface to configure what other WordPress roles are allowed to work with “Smart Manager”. So if you wish to use the full functionality of the “Smart Manager” consider to buy Pro version.
What to do if you wish to stay with free version of “Smart Manager”, but need make it available to the users without “Administrator” role? Is it possible to make free version of “Smart Manager” available for other user roles without changing its source code? Read more Unlock Smart Manager for WooCommerce Shop Manager

User Role Editor Pro version 4.8

User Role Editor Pro version 4.8 is available for download.
The list of updates includes:

  • Role ID validation rule was added to prohibit numeric role ID – WordPress does not support them.
  • HTML markup was updated to provide compatibility with upcoming WordPress 3.8 new administrator backend theme MP6.
  • It is possible to manage access of single sites administrators to the selected user capabilities and Add/Delete role operations inside User Role Editor.
  • Shortcode [user_role_editor roles=”none”]text for not logged in users[/user_role_editor] is available.
  • Other shortcode enclosed inside “user_role_editor” shortcode are processed recursively.
  • Gravity Forms available at “Export Entries”, “Export Forms” pages is under URE access restriction now, if such one was set for the user.
  • Gravity Forms import could be set under “gravityforms_import” user capability control.
  • Option was added to show/hide help links (question signs) near the capabilities from single site administrators.
  • Plugin “Options” page was divided into sections (tabs): General, Multisite, About.
  • Author’s information box was removed from URE plugin page.
  • Restore previous blog ‘switch_to_blog($old_blog_id)’ call was replaced to ‘restore_current_blog()’ where it is possible to provide better compatibility with WordPress API.
  • After use ‘switch_to_blog()’ in cycle, URE clears ‘_wp_switched_stack’ global variable directly instead of call ‘restore_current_blog()’ inside the cycle to work faster.

Do not use numeric role names

I do not recommend you to use numeric role names, e.g. ‘1000’, ‘150’, etc. You may create such role, may fill it with capabilities with the help of current and previous versions of “User Role Editor” and may be other plugins. But WordPress will not allow you to use such roles. You will get error message, like this: “You can’t give users that role”. Read more Do not use numeric role names

Shortcode for content view access restriction

Shortcode for posts/pages content view access restriction is available at User Role Editor Pro starting from version 4.7. Enclose restricted content with these shortcodes:
1) [user_role_editor roles="role1, role2"] some restricted content [/user_role_editor];
2) [user_role_editor except_roles="role1, role2"] some restricted content [/user_role_editor];
3) [user_role_editor users="user_id1, user_login1"] some restricted content [/user_role_editor];
4) [user_role_editor except_users="user_id1, user_login1"] some restricted content [/user_role_editor]
and you will restrict access to the part or the whole content of any post or page.

roles attribute: restricted content will be available for the logged-in user with one of the roles you input for the roles attribute of the [user_role_editor] enclosing tag. You should input the role identificator, not the role name. For example, valid shortcode will be [user_role_editor roles="contributor"], not [user_role_editor roles="Contributor"]. Pay attention to the lowercase 1st letter at “contributor”.
For all other users including the guest visitors restricted content, enclosed by the shortcode described above, will be hidden.
If you wish to show some content for not logged in (guest) visitors only, use this shortcode [user_role_editor roles="none"].

If you use a comma ‘,’ sign as a separator inside roles list: “role1, role2”, then ‘OR’ rule is applied: users with role1 OR role2.

If you use a ‘&&’ signs as a separator inside roles list: “role1 && role2”, then ‘AND’ rule is applied: user with role1 AND role2 simultaneously.

except_roles attribute: To show content inside shortcode to all logged in users except users with selected roles, included them into the ‘except_roles’ attribute. For example, if you don’t wish to show some text to the ‘subscriber’ role and other not logged in visitors use this shortcode:
[user_role_editor except_roles="subscriber, none"]some text to hide from subscribers[/user_role_editor].
‘none’ is a virtual role ID used by URE for not logged-in users

users attribute: To show content inside shortcode to logged in users with user ID or login inside comma separated list you can use this shortcode:
[user_role_editor users="18, 23, willy"]some restricted content[/user_role_editor]

except_users attribute: To show content inside shortcode to all users except users with user ID or login inside comma separated list you can use this shortcode:
[user_role_editor except_users="17, 25, peggy"]some restricted content[/user_role_editor]

In order to take into account not logged-in visitor you can use ID=0, like:
[user_role_editor except_users="0, 23, willy"]some restricted content[/user_role_editor]. This shortcode will show content inside to all logged-in users except user with ID=23 and user with login ‘willy’.

Other shortcodes enclosed inside User Role Editor shortcode are processed recursively.