Previously we already show you how to hide the admin bar from non admin user. Today, we are going to show you how to block the customer or non admin user from accessing the wp admin area. To be safe, hiding the admin bar is not enough, because if customer are WordPress user they might know the exact url. So, it’s always good to block non-privilege user to access the wp admin, especially your customer if your are using Woocommerce.
How to block Woocommerce customer from accessing WP Admin in WordPress
Advertisements
- Open your theme functions.php file and copy and paste the code below at the bottom of the file:-
/** * Block non-admin from accessing wp-admin */ function block_wp_admin_init() { if(is_user_logged_in()) { if (strpos(strtolower($_SERVER['REQUEST_URI']),'/wp-admin/') !== false) { if(!current_user_can( 'manage_options' ) ) { wp_redirect( get_option('siteurl'), 302 ); exit; } } } } add_action('init','block_wp_admin_init',0);
or there is another way using admin-init hook
/** * Redirect non-admin users to homepage * * This function call the 'admin_init' action hook. */ function redirect_non_admin_users() { if ( ! current_user_can( 'manage_options' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) { wp_redirect( home_url() ); exit; } } add_action( 'admin_init', 'redirect_non_admin_users' );
* if non admin user access wp-admin, the user will be redirect to home page again.
Related posts:
Virtue Theme: How to enable slider in shop page?
WordPress: How to create left sidebar template in TwentyEleven theme?
Woocommerce: How to bcc all order email to multiple recipients according to status
How to enable sidebar on product details page in Virtue Theme
FCKEditor - The full feature Web based WYSIWYG HTML Editor
How to write robot.txt to control search engine spider
How to disable access to Microphone or Camera in Chrome
WordPress Custom Taxonomy Pagination show 404 page not found error
Share this with your friends:-