How to hide WordPress admin bar with code

Open your theme’s function.php file. Add the following code at the end of your functions.php file. In the example below, the admin bar will only appear when the logged in user has role of “administrator”.