WP Statistics access for other roles

Administrator only has access to the WP Statistics WordPress plugin by default. Is it possible to allow access to WP Statistics for other roles? For the testing purpose we will give access to the “Statistics” menu of “WP Statistics” plugin to the user with “Editor” role. “Administrator menu access” additional module to the User Role Editor Pro will help to resolve this task.

1st, you need to activate “Administrator menu access” add-on. Go to the “Additional modules” tab of User Role Editor Pro Settings and turn on correspondent checkbox. Read more WP Statistics access for other roles

WordPress for registered users only

WordPress for registered users only. Is it possible? How to make blog content accessible for your visitors only after login? First way is to use some of existing plugins. Try to search for keyword “members” at WordPress plugins repository.
If you think that plugins you found are too heavy, or noisy with advertisement, or you wish some simple decision e.g. “install and forget”, try this one – in the form of the “must-use” plugin. It does not require the activation and has no any menu or settings page. Just place PHP file with the code below to the wp-content/mu-plugins/ folder. Read more WordPress for registered users only

Allow access to WordPress Appearance menu items

How to allow access for selected role to the selected menu items of the WordPress Appearance menu? The most menu items under “Appearance” menu at WordPress administrator back-end use the same “edit_theme_options” user capability to check if current user has access to it (read more…). Thus if user has access to the “Widgets” menu, he has access to the rest menu items: “Themes”, “Customize”, “Menu”, “Header”, “Background”. What to do if you wish to allow some role(s) change “Widgets”, “Menus” but prohibit access to other “Appearance” menu items? Read more Allow access to WordPress Appearance menu items

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.