Home > Dev > WordPress Customization > Restricting Access to the WordPress Dashboard

Restricting Access to the WordPress Dashboard

This code will restrict access to the WordPress dashboard based on user roles.

// Add this to your theme's functions.php file

function restrict_dashboard_access() {
    if (is_admin() && !current_user_can('administrator') && !(defined('DOING_AJAX') && DOING_AJAX)) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('admin_init', 'restrict_dashboard_access');
Back