What is wrong with this code to remove wp admin bar from one page

I’m just trying to turn off the wp admin bar on one page, but this function removes it from every page. What am I missing?

<?php 
  if ( !is_page('image-upload') ):
    show_admin_bar(false);
  endif;
?>

Related posts

Leave a Reply

3 comments

  1. You need to remove the (!) infront of the conditional, you can read about PHP-operators here.

    Now you simply say that if not on page “image-upload” remove the admin_bar.. Here is the working code:

    <?php 
    function wpse_80018_hide_admin_bar() {
       // If is on page "image-upload"
       // Remove the admin_bar
       if ( is_page('image-upload') ):
          show_admin_bar(false);
       endif;
    }
    add_action('wp_head', 'wpse_80018_hide_admin_bar');
    ?>
    

    Just an example to understand the conditional:

    <?php
    function wpse_80018_test() {
        // If iam on page "image-upload"
        // Echo honey i'm home!
        // If on another page echo "Working on another page!"
        if ( is_page('image-upload') ) {
           echo "Honey i'm home!";
        } else {
           echo "Working on another page!";
        }
    }
    add_action('wp_head', 'wpse_80018_test');
    ?>
    
  2. Hide Admin Bar for everyone

    This one is reasonably simple, to hide the WordPress Admin Bar for everyone, add the following to your theme’s functions.php file, the first bit hides the admin bar, the second bit hides the settings:

    add_filter( 'show_admin_bar', '__return_false' );
    
    function wp_hide_admin_bar_settings() {
        ?>
        <style type="text/css">
            .show-admin-bar {
                display: none;
            }
        </style>
        <?php
    }
    
    function wp_hide_admin_bar() {
        add_filter( 'show_admin_bar', '__return_false' );
        add_action( 'admin_print_scripts-profile.php', 
             'wp_hide_admin_bar_settings' );
    }
    add_action( 'init', 'wp_hide_admin_bar' , 9 );
    

    Hide WordPress Admin Bar for specific requests

    if ( isset($_GET['bar']) && 'no' == $_GET['bar'] )
       add_filter( 'show_admin_bar', '__return_false' );
    

    This would allow you to hide the WordPress admin bar by going to example.com/?bar=no, you can of course change those values.


    To hide the WP Admin Bar and the Admin Bar preference on his/her profile page for a specific user, add the following to your theme’s functions.php file:

    function wp_hide_admin_bar_settings() {
        ?>
        <style type="text/css">
            .show-admin-bar {
                display: none;
            }
        </style>
        <?php
    }
    
    function wp_hide_admin_bar() {
       if ( 2 == get_current_user_id() ) {
          add_filter( 'show_admin_bar', '__return_false' );
          add_action( 'admin_print_scripts-profile.php', 'wp_hide_admin_bar_settings' );
       }
    }
    add_action( 'init', 'wp_hide_admin_bar' , 9 );
    

    Or you could do the reverse, and enable the WordPress Admin Bar just for one user, by disabling it for everyone else:

    function wp_hide_admin_bar_settings() {
        ?>
        <style type="text/css">
            .show-admin-bar {
                display: none;
            }
        </style>
        <?php
    }
    
    function wp_hide_admin_bar() {
       if ( 2 != get_current_user_id() ) {
          add_filter( 'show_admin_bar', '__return_false' );
          add_action( 'admin_print_scripts-profile.php', 
              'wp_hide_admin_bar_settings' );
       }
    }
    add_action( 'init', 'wp_hide_admin_bar' , 9 );