+91 9737971210
info@wordpressdeveloperonline.com
Facebook
Facebook
Twitter
Twitter
Google+
Google+
LinkedIn
LinkedIn
Instagram
Instagram
WORDPRESS DEVELOPER
Menu
  • HOME
  • SERVICES
    • Free WordPress Setup
      • Free WordPress Website Blog
    • Hire WordPress Developer
      • WordPress Emergency Support
      • Hire PHP Developer
      • Hire WooCommerce Developer
      • Woocommerce Customization
      • Fix WordPress Errors
      • Fix WordPress Security
      • Multi Language Website Blog with WPML
    • Online Shopping Website
    • WordPress Theme Development
      • Avada Theme Experts
      • Divi Expert Services
      • Enfold Theme Expert
      • Jupiter Theme Expert
      • X Theme Expert Services
      • NewsPaper Theme Experts Services
    • PSD to WordPress
    • Speed Performance Optimization
    • WordPress Hosting Migration
    • SEO Link Building
  • PACKAGES
    • Speed Optimization Packages
  • WHY WE
    • PORTFOLIO
    • TESTIMONIALS
    • CAREER
  • BLOG
  • CONTACT

How to Block Dashboard Access for Non-Admins

September 20, 2018wordpressdeveloperWordpress

In WordPress, if you don’t want non-admins to even be able to access the wp-admin dashboard than here is solutions. This code will allow you to control if the admin bar should show for the user and if a user should be redirected when trying to access the dashboard.

Here i show you different case. Use it as per your requirement. Add this code in your theme’s functions.php file of your active child theme (or theme) and save the file.

Note : You can change the home_url() to something else if you want to redirect them somewhere else. For example, to redirect them to the shop page you can use home_url( ‘/shop’ ) or if you have a profile page home_url( ‘/profile’ ).

Removing the Admin Bar for Non-Admins

function hide_admin_bar( $show ) {
    if ( ! current_user_can( 'administrator' ) ) {
        return false;
    }
    return $show;
}
add_filter( 'show_admin_bar', 'hide_admin_bar' );

Block Dashboard Access for Non-Admins


function block_wp_admin() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
        wp_safe_redirect( home_url('/shop') );
        exit;
    }
}
add_action( 'admin_init', 'block_wp_admin' );

Redirecting users after login

function login_redirect( $url ) {
    return home_url( '/profile' );
}
add_filter( 'login_redirect', 'login_redirect' );

Redirecting users after logout

function logout_redirect( $url ) {
    return home_url( '/profile' );
}
add_filter( 'logout_redirect', 'logout_redirect' );