Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här inaktiverar du WordPress Admin Bar för alla användare utom administratörer

Vill du inaktivera admin bar i WordPress för alla användare utom administratörer?

Som standard kan du enkelt inaktivera WordPress admin bar för enskilda användare. Detta kan dock ta tid om du har många registrerade användare på din site.

I den här artikeln visar vi dig hur du enkelt inaktiverar WordPress admin bar för alla användare utom administratörer.

How to disable WordPress Admin Bar for All Users except administrators

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.

The WordPress admin bar

WordPress admin-verktygsfält har användbara genvägar till olika back-end-områden i WordPress, och dessa genvägar ändras baserat 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

Subscribe to WPBeginner

Om du föredrar skriftliga instruktioner är det bara att fortsätta läsa.

Metod 1: Inaktivera WordPress Admin Bar för enskilda användare

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.

Om du vill ta bort admin bar manuellt går du bara till sidan Användare ” Alla användare i WordPress adminpanel. Sedan hoverar du musen över den användare som inte behöver admin bar och klickar på ”Edit” när det visas.

How to edit a user's profile in WordPress

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).

How to hide the admin toolbar for non-admin users

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: Inaktivera Admin Bar för alla användare utom administratörer (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 admin bar genom att lägga till kod i filen functions.php, som är en fil i WordPress theme. Om du inte har gjort detta tidigare kan du kontrollera vår guide om hur du copy and paste code snippets i WordPress.

Vissa guider säger att du ska editera theme-filerna manuellt, men detta kan orsaka vanliga WordPress-fel och kan till och med helt förstöra din website.

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.

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.

Hiding the admin toolbar using WPCode

Here, you will see all the pre-made snippets you can add to your site. Detta inkluderar ett snippet som allow you att helt inaktivera comments, upload file types that WordPress doesn’t usually support, disable attachment pages, and much more.

På nästa skärm måste du hovera musen över alternativet ”Add Your Custom Code (New Snippet)” och klicka på knappen ”Use snippet” när den visas.

Adding custom code to your WordPress website with WPCode

På nästa vy skriver du in en titel för code snippet. Detta är bara för din referens, så du kan använda vad du vill.

Öppna sedan ”Code Type” dropdown och välj ”PHP Snippet”.

Adding a PHP snippet to WordPress using WPCode

När det är gjort klistrar du bara in följande kod i Editorn:

add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(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.

Automatically inserting code into a WordPress website

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.

Publishing a custom code snippet to WordPress

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: Inaktivera WordPress Admin Bar för alla användare utom Admins (ingen kod obligatorisk)

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.

Hiding the admin toolbar for specific user roles

När du har gjort det klickar du på ”Save Changes” för att lagra dina inställningar.

Metod 4: Inaktivera Admin Bar för alla användare (inklusive administratörer)

Vill du inaktivera admin bar för alla användare, inklusive site admins?

Du kan göra detta genom att lägga till kod som inaktiverar admin bar för alla som viewar din sites offentliga pages. Detta innebär att admin bar endast kommer att visas när du är i WordPress dashboard area.

För att göra det enkelt har WPCode det exakta code snippet du behöver i sitt built-in Snippet Library. Gå helt enkelt till Code Snippets Bibliotek.

The WPCode code snippet plugin for WordPress

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”.

The ready-made Disable The WP Admin Bar code snippet

Pluginet lägger automatiskt till koden på din site, ger koden en beskrivande titel, väljer korrekt infogningsmetod och lägger till och med till tags för att hjälpa dig att identifiera snippet.

Så här gillar du koden:

/* Disable WordPress Admin Bar for all users */
add_filter( 'show_admin_bar', '__return_false' );

Nu behöver du bara toggle omkopplaren från ’Inaktiverad’ till ’Aktiv’ och sedan clicka på ’Update’.

Adding custom code to WordPress

Om du nu besöker din websites front-end medan du är inloggad på ditt WordPress account, kommer admin bar att försvinna.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du inaktiverar WordPress admin bar för alla användare utom administratörer. Du kanske också vill se vår ultimata säkerhetsguide för WordPress och vår jämförelse av de bästa page builders för WordPress för att skapa anpassade layouts utan någon kod.

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.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

166 kommentarerLämna ett svar

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Destiny Boy

    This was very simple ans straight forward, it helped me

  3. ronald osoo

    This code has bug , its working but not bullet proof . The normal user can by -pass this and get access to the user admin like panel at the top by just adding wp-admin/ to the url like trying to login

    • Math

      I agree, direct access to wp-admin/profile.php is making this useless.

      • Steve

        A lot of websites not only from wordpress have the same option. You can acces to the admin-login page of any website just writing example.com/admin : Thats normal. I find it’s look really good without the adminbar.

  4. Alex S

    Hello,
    I wish your syntax highlighter was better. Tried to copy the code and got the numbers starting at 2. I don’t need your numbers, just the good code. I would use this site a lot more if this was not the case.

    Thanks.

    • WPBeginner Support

      You can also click on the little button on top right corner of the code snippet. It will open the raw code in a popup, which is much easier to copy.

      Administratör

      • stefano

        It works well, but the bar remains for visitors, who have not made the LOGIN

  5. Jenn

    This plugin was working great and I was using Theme My Login. When I deactivated that plugin it is not not functioning as before… I see the admin bar for all users. Any suggestions?

    • Jenn

      Sorry I meant to say this ’code’ was working great. I created a site-specific plugin as well and everything was working great. But after deactivating Theme My Login, not it seems to not have any effect and I see the admin bar on all users.

      Suggestions?

      • Jenn

        I just tried adding in the code for all users: show_admin_bar(false); and I can still see the admin bar.

  6. Alexander Pardo

    Thank you very much ! A very simple and clean solution. :)

  7. Jason A. Churchill

    I’m not well-read in editing the functions.php file. My first two attempts resulted in syntax error messages.

    Where exactly should I place this code in the functions.php file?

    • WPBeginner Support

      At the end. If your functions.php file ends with ?> then you need to paste the code just before it.

      Administratör

  8. Terry Hale

    Thanks so much for this! No plugins, nice, clean, simple.

  9. Bhris

    Great straight-to-the-point tutorial.

    Is there a version of the code that will block all non-users. That is, visitors to the site won’t see the bar, while all logged-in users would?

  10. Shashi

    I was trying to do what what was mentioned in the article and it did not work. I used the following instead which did the trick from me:

    if ( ! current_user_can( ’manage_options’ ) ) {
    show_admin_bar( false );
    }

    The above was mentioned in the wordpress codex: http://codex.wordpress.org/Function_Reference/show_admin_bar

    • vipin

      Thanks a lot its really cool…………

    • Karen

      Neither worked for me. I copied the numbers with the text as I did not know that I could
      click on the button on top right corner of the code snippet and copy the raw code. I removed the numbers, but overlooked the seven. I had to change the file entry in via my file manager as I could no longer edit the functions.php. So after a scare and a bit of back and forth I removed the number and the admin bar is still there. I also tried your code Shashi, but it’s not working either. :-(
      I have tried every plugin under the sun and nothing will remove the bar.

  11. kim04099

    Thank You!!

  12. Che Boielle

    Fastest fix to an odd issue I’ve ever found and implemented. Great explanation simple to follow instructions. Thanks guys, will be book marking your site.

  13. Manthan

    Thanks, Its the easiest way found..!!!

  14. Anup

    Why not simply do this for any specific user.

    • Jack

      It would be hard to do this if you have many users.

  15. Róger Marroni

    Great job man!

    thank you very much!

  16. Mega

    I tried and my site is black.

    • Ryan S

      the first step works fine, it is something in your end, maybe while implementing the code

  17. Etienne

    Perfect piece of code… Thanks a lot!

    Cheers

  18. Nathan Jansen

    Use this if you want this only for a certain role

    function remove_admin_bar() {
    $user = wp_get_current_user();

    if (in_array(’subscriber’, $user->roles)) {
    show_admin_bar(false);
    }
    }

  19. meef

    Godbless You. Answers to my problem always come from you post.

    Thanks

  20. Prem

    Finally I got the answers Thank you

  21. Paw Pet Directory

    I used this on my directory for my customers but the bar will not go away for them. I am using 3.8 with AppThemes Vantage Theme (Latest)

  22. Cory

    This is one of those things that, because it’s such a small customization, I’d hate to use a plugin for. So being able to throw this short piece of code into my child theme’s functions.php made me super happy. Thanks so much far sharing! Works like a charm.

  23. Raman

    Thanks for posting it was useful.

  24. Naveen

    I have added this code it is working fine, but if a user takes meta there is an option yo view the admin, if i click on this the admin page can see, how to avoid this, i dont want to give admin panel to the users, a user can edit the url and give /wp-admin also he can see the admin, i dont want both what will do for this?

  25. Subodh

    Admin bar is hidden but now how can a user log out?
    I am using bbpress forum and a plugin for users to log in via their Facebook account to join forum. But find no option for logging out.

  26. Loralee

    Thanks for the great tutorial. I thought it didn’t work – and then silly me I hadn’t followed the last step to activate my new plugin!! Thanks so much for this! So much better than installing multiple new plugins.

  27. vikas

    Thanks mate….u were really helpful…
    tc :-)

  28. Rajat

    Thx syed it just worked perfectly

  29. Bongo

    Thanks for this, it helped me a lot

  30. Andy Wagstaff

    I have an area of my site that requires log-in registration.
    I have made the admin bar ”autohide” by requiring a plug-in, I don’t want to entirely disable it, as I need the abiity to change password, but is there any way of removing items from the bar for users? I don’t want the site name,WP logo or the theme extensions options on the left hand side.

    Is this possible? I am sure it is, but I can’t work it out!!

    Ta!! Really handy site by the way

    Andy

  31. Rajiv

    Hi,

    I want to disable BP Admin Bar for Logged Out Users.

    Please help.

  32. Ken Shoufer

    Hello,

    Your code worked fine when I tried it locally on my computer, but when I tried it on a fresh WordPress install on my host server it would not let me log out. When I tried to log out, the address showing was ”http://www.ken-shoufer.com/wordpress1/wp-login.php?action=logout&_wpnonce=9e7f51a307”.

    I’m not sure what is wrong.

  33. Nate

    i am trying to disable the W logo on the top left, the ”My Sites” menu and the dashboard link on a buddypress multisite install. I want to keep the right side functional which shows the links to their buddypress profile settings. How can I achieve this?

  34. Tanner Moushey

    To avoid the blank space where the admin bar was, call the function in the ”after_setup_theme” action instead of ”init”.

    • Cristian O. Balan

      Really perfect, thanks a lot!

    • Will

      Thank you so much! This worked perfectly and resolved several hours of head-scratching.

  35. Cristian B.

    Really nice code. Are here also a fix to the blank bar that many here complains?

    • David

      Hi,
      To avoid the blank, just instead of :
      add_action(’init’, ’remove_admin_bar’);
      use:
      add_action(’after_setup_theme’, ’remove_admin_bar’);

      • Cristian O. Balan

        Thanks a lot man!

  36. niki

    Fantastic! Except I also have the blank space visible where the admin bar would be. Does anyone have a fix for this?

  37. Marlita Hill

    Hello. Thank you for this. How would I write the code if I only want the bar accessible to admin and contributors? I still do not want the contributors to have admin access to my backend.

  38. Steve

    Well… it removes the admin bar but it´s still occupying the space meaning there´s too much space up there now. I´m not sure it´s supposed to be that way? ;-)

  39. Adam

    worked …
    Thanks a lot

  40. Nick Cokas

    Add the code and worked perfect. However, the user still sees a white space above were the toolbar existed before. Any ideas to remove that.

    Thanks

  41. Toby Couchman

    Did you use a plugin to place all the necessary user stuff on the frontend?

    My team recently built a site with similar requirements and I’d love to compare the processes.

    Cheers
    Toby

    • Editorial Staff

      Agreed. Just updated the code to make it into a function, and then adding it in via action.

      Administratör

  42. Sally

    Excellent post thanks so much!

    Took me under a minute and the bar was gone… forever… truly grateful.

    Sally :) x

  43. Corey Freeman

    Thanks to this I have one less plugin bloating my site. Woo!

  44. Delton Childs

    This is an awesome post. short, to the point and contained a useful snippet! Thank you so much!

    I’m using the S2Member plugin also, I was very concerned about compatibility issues with plugin based solutions because of the various s2Membership Levels. This solutions is working wonderfully for me.

    Thanks Again!

  45. James Davis

    Hi Guys,

    Just found this bit of code and it worked perfect for my site…

    Thanks so much

    Cheers

    James

    AKA The Fitness Blogger

  46. glueckpress

    Nice. For Superadmins in a Multisite install it would read like if(!current_user_can(’manage_network’)) //etc.

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.