WordPress 為了方便管理員快速的從前台進入後台來管理網站在 WordPress 頂部強制加入了一個工具條 (admin bar),而且默認是對所有用户都顯示的,有時候看着挺煩心。那麼怎麼來去除這個煩人的工具條 (admin bar) 呢?下面小編上代碼。

一、完全禁用工具條:

1 、完全去除 WordPress 工具條 (代碼一)

show_admin_bar(false);

完全去除 WordPress 工具條 (代碼二)

add_filter('show_admin_bar', '__return_false');

2 、只對特定用户顯示工具條

只對管理員顯示

if (!current_user_can('manage_options')) {
    add_filter('show_admin_bar', '__return_false');
}

只對管理員和編輯顯示

if (!current_user_can('edit_posts')) {
    add_filter('show_admin_bar', '__return_false');
}

3 、將工具條從頂部移至頁腳

function fb_move_admin_bar() {
    echo '
    <style type="text/css">
    body {
    margin-top: -28px;
    padding-bottom: 28px;
    }
    body.admin-bar #wphead {
       padding-top: 0;
    }
    body.admin-bar #footer {
       padding-bottom: 28px;
    }
    #wpadminbar {
        top: auto !important;
        bottom: 0;
    }
    #wpadminbar .quicklinks .menupop ul {
        bottom: 28px;
    }
    </style>';
}
// 如果你想讓工具條顯示在後台頂部,請刪除這行代碼
add_action( 'admin_head', 'fb_move_admin_bar' );
//如果你想讓工具條顯示在前台頂部,請刪除這行代碼
add_action( 'wp_head', 'fb_move_admin_bar' );

以上代碼都是加入到 functions.php 中即可。