Vissa läsare har frågat oss hur man inaktiverar WordPress-adminfältet. Även om det är en liten justering, föredrar vissa användare att göra detta av olika skäl, till exempel för att upprätthålla ett renare användargränssnitt eller förbättra användarupplevelsen för användare som inte är administratörer.
Som standard kan du enkelt inaktivera WordPress-adminfältet för enskilda användare. Detta kan dock vara tidskrävande om du har ett stort antal registrerade användare på din webbplats.
I den här artikeln visar vi dig hur du enkelt inaktiverar WordPress admin bar för alla användare utom administratörer.
Vad är admin bar i WordPress?
Som standard visar WordPress en admin bar högst upp på vyn för alla inloggade användare. När du är inloggad på ditt account kan du se detta admin-verktygsfält i WordPress admin area och på alla andra pages.
WordPress adminverktygsfält har användbara genvägar till olika backend-områden i WordPress, och dessa genvägar ändras beroende på en användares roll och behörigheter i WordPress.
Admin bar kan dock vara distraherande när du tittar på front-enden av din website.
Det kan också påverka din websites design och användarnas upplevelse. Detta kan vara ett problem om du bygger eller hanterar en WordPress site för en tredje part, eftersom det hindrar dem från att se hur deras website verkligen gillar att se ut.
Lyckligtvis finns det flera sätt att inaktivera admin bar för alla användare utom administratörer. Använd bara snabblänkarna under för att hoppa till den metod du vill använda:
Video Tutorial
Om du föredrar skriftliga instruktioner är det bara att fortsätta läsa.
Metod 1: Ändra varje användares behörigheter i WordPress
Du kan inaktivera admin bar för specifika användare genom att helt enkelt editera deras användarprofil. Det här är en snabb och enkel metod om du bara behöver ta bort fältet för ett litet antal personer. Men om du runar en membership site med många användare rekommenderar vi att du väljer en annan metod.
För att ta bort adminfältet manuellt går du bara till sidan Användare ” Alla användare i WordPress adminpanel. Håll sedan muspekaren över den WordPress-användare som inte behöver adminfältet och klicka på ”Redigera” när det visas.
Detta kommer att öppna den användarens profil.
Avmarkera sedan boxen bredvid alternativet ”Show toolbar when viewing site” (Visa toolbar när du viewar webbplatser).
Efter det rullar du längst ner på vyn och klickar på ”Update User” för att save your changes. Detta inaktiverar admin bar endast för den specifika personen.
För att dölja toolbaren för fler användare följer du bara samma process som beskrivs ovan.
Metod 2: Använd kod för att inaktivera Admin Bar för icke-admin-användare (rekommenderas)
Om du behöver dölja admin bar för många olika personer, skulle det ta mycket tid och ansträngning att ändra varje användares inställningar manuellt.
Av den anledningen rekommenderar vi att du inaktiverar adminfältet genom att lägga till kod i filen functions.php, som är en temafil för WordPress.
Vissa guider säger att du ska redigera temafilerna manuellt, men detta kan orsaka vanliga WordPress-fel och kan till och med helt förstöra din webbplats.
Av den anledningen rekommenderar vi att du använder WPCode. Det är det bästa code snippet plugin och gör det enkelt att add to custom kod i WordPress utan att sätta din site i riskzonen.
Vi har använt WPCode för att ändra administratörens färgschema, ta bort texten ”Howdy Admin”, inaktivera skärmalternativsknappen och mycket mer.
Först måste du installera och aktivera det gratis tillägget WPCode. För mer information, se vår steg-för-steg guide om hur du installerar ett plugin för WordPress.
När plugin är aktiverat, gå till Code Snippets ” Add Snippet.
Här ser du alla färdiga snippets som du kan lägga till på din webbplats.
Faktum är att WPCode har den exakta kodsnutten du behöver för att inaktivera adminfältet i sitt inbyggda snippetbibliotek. Gå helt enkelt till Kodsnuttar ” Bibliotek.
Här söker du efter ”Inaktivera WP Admin Bar”.
När rätt snippet dyker upp klickar du bara på knappen ”Använd snippet”.
Pluginet lägger automatiskt till koden på din webbplats, ger koden en beskrivande titel, väljer rätt insättningsmetod och lägger till och med till taggar för att hjälpa dig att identifiera utdraget.
Så här gillar du koden:
/* Disable WordPress Admin Bar for all users */
add_filter( 'show_admin_bar', '__return_false' );
Om ditt mål nu är att inaktivera adminfältet för alla användare behöver du bara växla från ”Inaktiv” till ”Aktiv” och sedan klicka på ”Uppdatera”.
Men eftersom vårt mål är att inaktivera WordPress-adminfältet för användare som inte är administratörer, måste vi göra några små justeringar i koden.
Du kan ersätta den befintliga koden med följande:
/* Disable WordPress Admin Bar for all users except administrators */
add_filter( 'show_admin_bar', 'restrict_admin_bar' );
function restrict_admin_bar( $show ) {
return current_user_can( 'administrator' ) ? true : false;
}
Den här koden identifierar alla användare som inte är administratörer och som för närvarande inte tittar på adminpanelen. För dessa användare inaktiverar den WordPress admin bar.
Efter det rullar du ner på sidan till sektionen ”Insertion”. Här kan du behålla standardmetoden ’Auto Insert’ för att se till att koden körs överallt.
Slutligen rullar du till högst upp på vyn och clickar på slidern ”Inaktiverad” så att den visar ”Aktiv”.
Sedan klickar du bara på knappen ”Save Snippet” eller ”Update” för att göra code snippet live.
Nu är det klart! Kom bara ihåg att kontrollera din WordPress website för att se till att allt fungerar som det ska.
Metod 3: Använd ett gratis plugin för att inaktivera Admin Bar för icke-admin-användare
Om du inte vill add to kod till din website, kan du dölja admin bar med hjälp av ett plugin. Hide Admin Bar Based on User Roles låter dig ta bort toolbaren baserat på olika användarroller, så det här är ett bra val om du vill inaktivera baren för alla medlemmar, WooCommerce customers eller någon annan användarroll.
Först måste du installera och aktivera pluginet Hide Admin Bar Based on User Roles. För mer detaljer, se vår Step-by-Step guide om hur du installerar ett WordPress plugin.
När du är aktiverad måste du gå till Settings ” Hide Admin Bar Settings page. Härifrån kontrollerar du boxarna bredvid de användarroller där du vill inaktivera admin bar.
När du har gjort det klickar du på ”Save Changes” för att lagra dina inställningar.
Vi hoppas att den här artikeln hjälpte dig att lära dig hur du inaktiverar WordPress adminfält för alla användare utom administratörer. Du kanske också vill se vår guide om hur du får e-postmeddelanden för inläggsändringar i WordPress och våra expertval av de bästa plugins för aktivitetslogg och spårning i WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Hussam H
Thank you and bless you.
WPBeginner Support
You’re welcome!
Administratör
Paul
Using the after_setup_theme hook doesn’t always work. Try this instead:
function remove_admin_bar( $value ) {
return ( current_user_can( ’administrator’ ) ) ? $value : false;
}
add_filter( ’show_admin_bar’ , ’remove_admin_bar’);
Matthew D Henderson
Thank you for providing four different ways to do this. Perfect
WPBeginner Support
Glad you found our recommendations helpful
Administratör
Joey
What would be the code if I want to add Editor and Admin to show the Admin bar..
if (!current_user_can(’administrator’) && !is_admin())…
if (!current_user_can(’editor’) && !is_editor())…
would it be if (!current_user_can(’administrator’) && !is_admin() || !current_user_can(’editor’) && !is_editor())
would this work?
WPBeginner Support
For what you are wanting, you would want to remove: && !is_editor()
Then it should work how you are wanting.
Administratör
Basia
Hi,
I hide admin bar with plugin You recommend, but when subscriber log in he can still click on ”view my profile” and see wp dashboard. Can I disable that?
WPBeginner Support
You can redirect users after login using the method from our guide below:
https://www.wpbeginner.com/plugins/how-to-redirect-users-after-successful-login-in-wordpress/
Administratör
Deewinc
Thanks for the article but method 3 doesn’t work.
WPBeginner Support
If method 3 is not working for you, we would recommend trying one of the other methods to test.
Administratör
Dipesh Vedak
what if theme upgrades?
WPBeginner Support
If you created a site-specific plugin it would remain.
Administratör
Brandon Porter
Worked like a charm. Thank you!
WPBeginner Support
You’re welcome, glad our guide was helpful
Administratör
Bryan E Jackson
Doesn’t seem to work for my Divi Theme
WPBeginner Support
You may want to try clearing your cache for the most common reason for the change not being visible.
Administratör
Paul
What exactly does this disable? I still see an admin bar and I have access to menu options when I go to example.com/wp-admin when i’m logged into a subscriber account.
WPBeginner Support
You would want to ensure the code was properly added for the most likely cause of the admin bar not being hidden otherwise, you could also have a plugin that would be overriding this code.
Administratör
Jürgen
Thanks. Just what I needed
WPBeginner Support
Glad our guide was helpful
Administratör
khaoula
Thanks a lot ,but what to do if the user wants to logout ?
WPBeginner Support
You can add a logout link to your menu for one option, we have a guide on how to do that below.
https://www.wpbeginner.com/wp-tutorials/how-to-add-the-wordpress-logout-link-to-navigation-menu/
Administratör
Kaetech
Thanks a lot. Just what I needed. It worked.
WPBeginner Support
You’re welcome, glad our guide was helpful
Administratör
Marc Korden
Cannot add the admin code in my theme:
Communication with the site not possible to check for errors, the PHP adjustment has been reversed. The PHP file change needs to be changed in another way, for example using SFTP.
WPBeginner Support
That message means that WordPress was not able to check the code for errors and you would need to use an FTP tool to add the code. We have a guide you can follow below:
https://www.wpbeginner.com/beginners-guide/how-to-use-ftp-to-upload-files-to-wordpress-for-beginners/
Administratör
Jailson Pacagnan Santana
Thanks! God bless you
WPBeginner Support
You’re welcome, glad our guide was helpful
Administratör
Ciao
Where should i exactly insert the code, at the beginning, at the end of funcions.php? thx
WPBeginner Support
We normally recommend at the end so it is easy to find and remove if needed
Administratör
Agha Mubasher
Hello there…!
First of all i simply love the work wpbeginner, as you always bring forward the simplest solutions to our wordpress issues. Your website and Youtube channel has always been helpful for me. Thumbs Up for that..
I had issue with hiding the admin bar for the subscribers only. Now after applying your code in the function.php it is hidden for my editors also.
Is there any way that my editors also can see the admin bar and only it should be hidden from the subscribers..!!
WPBeginner Support
For that, you would need to target another permission that your editor has instead of what we are targeting such as edit_others_posts
Administratör
Felix
Thank you very much.
WPBeginner Support
You’re welcome
Administratör
Dana Jewel
Thank you! This snippet has been really, really helpful.
WPBeginner Support
You’re welcome, glad our guide could be helpful
Administratör
Faris
Great Help, It worked like a charm!
Thank You so much.
WPBeginner Support
You’re welcome
Administratör
Jesaja
On my website it says:
”Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”
What to do now?
WPBeginner Support
It means the new WordPress safety features prevented you from editing the file, you would need to download and edit your theme’s files using FTP: https://www.wpbeginner.com/beginners-guide/how-to-use-ftp-to-upload-files-to-wordpress-for-beginners/
Administratör
Jesaja
Oh.. can’t I just put it in the Custom CSS area?
WPBeginner Support
No, this is PHP not CSS so it wouldn’t go into there.
TaiRon
It does work like a chame, your code is perfect but I have to change in file manager directly.
Thanks.
WPBeginner Support
You’re welcome, glad our guide could help
Administratör
Bongani
Thanks , very useful tutorial
WPBeginner Support
You’re welcome
Administratör
Mike
Thank you!
WPBeginner Support
You’re welcome
Administratör
Jim
Can this code be altered to allow the WordPress Admin bar for Admins AND moderators, but hide it for everyone else?
Thanks!
WPBeginner Support
You would need to add another && !current_user_can(”) inside the parentheses with the name of the role you are wanting it to appear on or use the plugin adminimize from our article: https://www.wpbeginner.com/wp-tutorials/what-everybody-ought-to-know-about-the-wordpress-admin-bar/
Administratör
Andreas
If a user knows the URL structure of WordPress he can easily browser to /wp-admin/ and there the admin toolbar will be visible. Also, the easiest way is to deactivate it in the user setting if you have a small number of users.
Adam Pressman
Doesn’t work. Well, to be fair, works when you first use it but then something happens and it doesn’t anymore. nothing is overwriting the functions.php and the code is there but usually after the first login, logout cycle a subsequent login will see the admin bar return.
prashanth
It works but user cannot logout. How to logout?
Cesar
I have created a social media site using several plugins and everything works fine but for one issue. Pages are successfully restricted and redirected (Buddypress pages, blogs, etc) for non-logged in users.
However the main issue I am having is that non-logged in users can still have access to blog posts and user profile pages via widgets placed on side bar and footer and I cannot seem to find a solution anywhere.
Any ideas on the matter, plugins or code that I can insert in the child theme?
I have been looking for a solution for over a day and all there is there is how to restrict pages and partial content but nothing works on the widget links, they keep going thru the restrictions in place.
Thanks
WPBeginner Support
Hey Cesar,
There are several widgets and plugins that allow you to hide widgets from non-logged in users. However, if non-logged in users know the URL, then they would still be able to access those URLs directly. You need to review your plugin settings and see if you can find the option to hide profile pages from non-logged in users.
Administratör
Shabz
This code or several others that I tried from the comments isn’t working. I’m trying to remove that dumb silver/black wordpress tab above my menu that shows up for all of my website’s users. How do I remove that? Please help
Humberto Buitrago
Is there any plugin we can use instead code?
Thanks in advanced!
Jack Hernandez
As mentioned in the article, you included a way for users to edit their profiles through the front end without the admin bar. How did you accomplish that.
Thanks
Paritosh Negi
Thank you so much! <3 it works well
Chouchouda
Hello, some issue , i have done everything but can’t hide toolbar for owner user in frontend , but can do this for admin user , incredible.
I’m using search& go theme wordpress , Thank for your help.
Alex
Hello,
Code needs a little updating. This is the code I would use to redirect by role.
/*Hide admin bar for certain roles*/
function hide_admin_bar() {
if(is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( in_array( ’subscriber’, (array) $current_user->roles ) ) {
add_filter(’show_admin_bar’, ’__return_false’);
}
}
}
Hope this helps.
Brandon
Thanks for this updated code, Alex. Simple and works great.
Put it in child theme functions.php and forget about it.
Yuki Zain
Thanks, Alex but I’m editing a bit
/*Hide admin bar for certain roles*/
if(is_user_logged_in() ) {
$current_user = wp_get_current_user();
if ( in_array( ’subscriber’, (array) $current_user->roles ) ) {
add_filter(’show_admin_bar’, ’__return_false’);
}
}
randalf
Very nice but what do I do if I have two roles as a result of bbpress plugin.. That is registered users have the subscriber role in whole site and participant role in bbpress.
Please help
Scott
Hi,
I inserted your code to remove the Admin bar from my membership site. Unfortunately it also removed it from me, the Admin (even thought I used your first option above). I am using the Tesseract Pro theme. Do you have any suggestions? Thanks
Inder Singh
Dear Sir,
I have 2 admin user and i want to show admin bar for first user and remove admin bar for second user in wordpress.
How can i do it.
Thanks for support.
SG
I tried using this and I got that code Parse error: syntax error, unexpected ’}’ in /home/content/76/10323476/html/wp-content/themes/digitalscience-apex/functions.php on line 168 and I can’t get my site back even after deleting it.
Krzysiek Dróżdż
Well, I wouldn’t use current_user_can for that… Codex says, that you can use it for role checking, but in the code you can find:
* While checking against particular roles in place of a capability is supported
* in part, this practice is discouraged as it may produce unreliable results.
So… Much better and secure way to do this is:
$user = wp_get_current_user();
if ( ! in_array( ’administrator’, (array) $user->roles ) ) {
…
}
Rajdeep dey
This code not working on my website… Did any one have any other solutions for hiding admin bar.
Lyndal Sirit
i used it and it crashed my site, trying desperately to fix it now, i took the code back out and updated but I am getting an Error: Parse error: syntax error, unexpected ’3’ (T_LNUMBER) in /home/lyndalspirit/public_html/wp-content/themes/primer/functions.php on line 516
WPBeginner Support
Hi Lyndal,
Some times when users copy code from websites like WPBeginner, they also copy the line numbers which they are not supposed to copy. When they paste this code in their functions.php file it causes an error.
You need to connect to your site using an FTP client. Locate your functions.php go to the code you added and remove it. Save your changes.
Administratör
SG
Even after deleting it, my site is coming back HELP!
Chris
This worked fine for me as-is, i stripped out the line numbers and stuck it at the end of my theme functions.php – instant success. thanks so much.
WPBeginner Support
Hey Chris,
Glad you found it useful. Don’t forget to join us on Twitter for more WordPress tips and tutorials.
Administratör
Job
The Code works like charm. Thanks.
eli
if(!current_user_can(’administrator’)) {
add_filter(’show_admin_bar’, ’__return_false’);
}
Gordon Cockburn
How do I remove an individual ex member from access to members only area of website
WPBeginner Support
Login to your WordPress admin area using an Administrator account. Click on the Users menu item from the admin sidebar. This will show you a list of users registered on your WordPress site. Locate the user you wish to remove. Click on the Delete link below the username of the person you want to remove.
Administratör
Jguiss
Didn’t worked for me…
It’s worked with that : add_filter(’show_admin_bar’, ’__return_false’);
JGUISS
JGUISS
Didn’t worked for me…
It’s worked with that : add_filter(‘show_admin_bar’, ‘__return_false’);
how does the code look like with this added I’m not familiar enough with php to add it in
The Little Binger
Hello!
Thank you so much for being such a great help! I installed this code on the function file but it does not seem to work. I loaded my page on a different browser but the Log In bar is still there. Why is that? I also read your article about adding codes to the PHP file.
I hope you could help me out on this. Thanks!!
Vic
Hi! First of all, thanks for these codes, it’s very helpful, provided I considered myself not a first timer anymore who remember to add after the codes!
I locked myself at the first time trying to add the php coding in my functions.php file as well, thanks to the ”expert” web developer who didn’t show the full set of codes for a function. And thanks to your ”what to do when you are locked out of WordPress admin area” site, I found out why I was locked out in the first place! But it took me a downtime of 3 days to figure out how to use the FTP, which at the end failed to function, but my webhost Helpdesk suggested me to use the File Manager in their Control Panel instead! Luckily it works! Lessons learnt the hard way, but worth it..
Just a kind suggestion, since this site was supposed meant for ”WPBeginner”, I think all of the WP users beginner would appreciate if full set of codes are provided, rather than every other person ”shouting” in the comment section that ”The codes didn’t work, and locked me out”, and then you have to advise them to read a full length of another tutorial how to unlock their website, even though yes, you wish to teach us ”How to fish” instead of ”Fish for us” every time!
Anyway, thanks again and appreciate your efforts here in guiding us, the WP Beginners!
WPBeginner Support
Thanks for the feedback. We try to make code easy to paste and use. However, usually there is already code in your functions.php file, which may affect the end result. We are glad you found your way out.
Administratör
Louis
It works just perfect!
Easy and functional.
Thank u!
lucas
Yo your code line has ruined both of my sites I cannot acces the wp-admin at all I get a fatal error message.. how can I fix this please
WPBeginner Support
You need to remove the code you added. See our guide on what to do when locked out of WordPress admin area.
Administratör
Peters A P
I did what was mentioned on the link you send me.
Now the entire site is gone
I cannot see anything, please check for yourself and see.
WPBeginner Support
Please see our guide what to do when you are locked out of WordPress admin area
Administratör
lucas
Hi, did you manage to fix the problem? I had the exact same issue