Take Control of User Roles in WordPress

Публикувано на 23.05.2017
Take Control of User Roles in WordPress

Introduction

Today we’ll look at several useful WordPress functions that allow you to take control over user access and customize the admin panel.

For example, you might want to give a client access to edit content but not to install plugins or modify themes.

💡 Tip: Always keep full administrator rights for yourself — most users don’t need them.


Hide Admin Menu Pages

Let’s say you want to hide the Plugins section for a specific user.
Here’s how you can do it:

function remove_menu_pages() {
    $user = wp_get_current_user();
    if ($user->nickname == 'pe6o') {
        remove_menu_page('plugins.php');
    }
}
add_action('admin_menu', 'remove_menu_pages', 101);

If the logged-in user is “pe6o,” the Plugins menu will be hidden.


Disable File Editing

To prevent anyone from editing plugin or theme files through the dashboard, add this line to your wp_config.php file:

define('DISALLOW_FILE_EDIT', true);

Remove Submenu Pages

You can also remove submenu items. For example, to remove Menus from the Appearance section:

remove_submenu_page('themes.php', 'nav-menus.php');

Remove Dashboard Meta Boxes

If you want to declutter the dashboard, you can hide the WordPress news widget and others:

function cw_remove_dashboard_element() {
    remove_meta_box('dashboard_primary', 'dashboard', 'side');
    // remove_meta_box('dashboard_activity', 'dashboard', 'normal');
    // remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
}
add_action('admin_init', 'cw_remove_dashboard_element');

Conclusion

I personally prefer keeping such tweaks as separate plugins instead of mixing them with the theme functions.
It’s cleaner and easier to maintain.

If you have questions or feedback — feel free to reach out through the contact form!