Hide WordPress Adminbar for Untrusted User
- Article
- Comment
Hide WordPress Adminbar for Untrusted User is one of a security to avoid users to access your wp-admin from the front end. You can simply disable it by using a following function. But remember the style-sheet of the adminbar is loaded unnecessarily and take some page load time which will reduce your site speed.
show_admin_bar( false );
just adding the above line of code on your WordPress theme header help you to disable the adminbar. But the stylesheet still loads and which gives some added weight to load your site.
As like the above image, the two style sheets load always and take your site to load more and take time to render it. So now we are going to de-queue the style for not logged in users. Add the following code on your theme “ functions.php ” file
The following function help you to disable adding style sheets for not loggend in users.
################################################################################ // Disable adminbar with disabling stylesheet. ################################################################################ if(is_user_logged_in() ) { add_action ( 'init', 'kv_adminbar_front_kill' ); function kv_adminbar_front_kill() { show_admin_bar( false ); wp_dequeue_script('admin-bar'); wp_dequeue_style('admin-bar'); } }
Likewise you can prevent loading based on your role and restrict to load for admins only.