search
HomeBackend DevelopmentPHP TutorialDisable Admin Bar in WordPress 3.3: Step-by-Step Guide

WordPress (Sonny) Version 3.3 was released just last night. Half asleep, I downloaded the new version, set up the database, and installed it. A novel welcome method, I like it. Then I configured my settings the way I like it. Fix your banner, set up your permalinks (look, %postname%, awesome, awesome), format date and time, and more. Then I disabled the admin bar. Click "Update Profile." great! Hey, wait a minute. Is the admin bar still there? ? What [insert expletive]!


Why can't I get rid of the admin bar?

In new WordPress versions, the WordPress core developers decided that the admin bar is an important part of the admin section (from what I understand). Personally I don't think it's of much use. To me, it's just an ugly bar at the top of the page with only some options found on the side menu. No, I have an unbearable hatred of admin bars. I just don't like it.

With previous versions of WordPress, you could simply go to your user profile to disable the admin bar, or you could use this popular code in your functions.php file.

add_filter('show_admin_bar', '__return_false');

With the release of version 3.3, we no longer provide the option to disable the admin bar in the admin section. I applaud the WordPress core development team for trying to make WordPress easier to use and access, but something seems to be missing from their thinking. People like to have choices, but they don't like having those choices taken away from them. Especially when they use these options.

So, I quickly came up with this solution to disable the admin bar.


Disable admin bar in admin section

This code will be placed in the functions.php file, so go ahead and open it in your favorite text editor.

First let's set up the function by checking.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
  }

}

This will ensure that the function "disableAdminBar" does not already exist. If not, our function will run.

Next, let's remove the action that enables the admin bar.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
  	remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  
  }

}

Now, for the action hook that initializes the disableAdminBar function.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
  	remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  
  }

}

add_filter('admin_head','remove_admin_bar_style_backend');

Okay, so this disables the admin bar, but still has 28 pixels of padding at the top of the page.

在 WordPress 3.3 中禁用管理栏:分步指南

You can go into the admin-bar.css file and edit the css in it to remove the padding. The css property you want to edit looks like this.

body.admin-bar #wpcontent,
body.admin-bar #adminmenu{
padding-top:28px;
}

You can dig into the wp-admin.css file and change the css in it, but I think it's best to leave the core file alone. So I'm going to override the css in the function we just created. This is the function we will use to override the css.

function remove_admin_bar_style_backend() { 
  echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
}
      
add_filter('admin_head','remove_admin_bar_style_backend');

Here is the complete function to disable the admin bar and override the css.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
  	remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
  
    function remove_admin_bar_style_backend() { 
      echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
    }
          
    add_filter('admin_head','remove_admin_bar_style_backend');
  
  }

}

add_filter('admin_head','remove_admin_bar_style_backend');

Now the 28px padding should be gone.

在 WordPress 3.3 中禁用管理栏:分步指南

It's not pretty, but it works, and the css can be overridden this way too. If you want to re-enable the admin bar. You don't have to change core CSS back.


Disable admin bar in admin section

If you want to disable the admin bar on the front-end of your website, you can go into your user profile and uncheck "Show toolbar when viewing the site". Pretty simple, but if you want to disable the admin bar all together, we'll have to add more to the "disableAdminBar" function.

This is what disables the front-end admin bar.

	remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 );

In our "disableAdminBar" function. It looks like this.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
    remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 ); // for the admin page
    remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // for the front end
  
    function remove_admin_bar_style_backend() { 
      echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
    }
          
    add_filter('admin_head','remove_admin_bar_style_backend');
    
	}

}

add_filter('admin_head','remove_admin_bar_style_backend');

The admin bar disappeared, but there was a problem. Your site now has a 28 pixel margin at the top.

在 WordPress 3.3 中禁用管理栏:分步指南

This is tricky. This css is hardcoded in when "Show toolbar

when viewing site" is checked. I can't figure out how to remove the CSS so we have to override it like using the admin part of this function.
function remove_admin_bar_style_frontend() { 
  echo '<style type="text/css" media="screen">
  html { margin-top: 0px !important; }
  * html body { margin-top: 0px !important; }
  </style>';
}

add_filter('wp_head','remove_admin_bar_style_frontend', 99);

The 99 at the end of the add_filter hook is to ensure that the css in

is after the original hardcoded css in

在 WordPress 3.3 中禁用管理栏:分步指南

This is our completed function.


if (!function_exists('disableAdminBar')) {

	function disableAdminBar(){
  
  	remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 ); // for the admin page
    remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // for the front end
  
    function remove_admin_bar_style_backend() {  // css override for the admin page
      echo '<style>body.admin-bar #wpcontent, body.admin-bar #adminmenu { padding-top: 0px !important; }</style>';
    }
          
    add_filter('admin_head','remove_admin_bar_style_backend');
    
    function remove_admin_bar_style_frontend() { // css override for the frontend
      echo '<style type="text/css" media="screen">
      html { margin-top: 0px !important; }
      * html body { margin-top: 0px !important; }
      </style>';
    }
    
    add_filter('wp_head','remove_admin_bar_style_frontend', 99);
  
  }

}

// add_filter('admin_head','remove_admin_bar_style_backend'); // Original version
add_action(‘init’,'disableAdminBar’); // New version

in conclusion

This may not be the most convincing solution, but it works as a quick fix for now. I can’t wait to see what other solutions and WordPress tips are coming soon.

Well, WordPress version 3.3 (aka Sonny) is finally here. If you haven't upgraded yet, please upgrade now.

The above is the detailed content of Disable Admin Bar in WordPress 3.3: Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool