删除特定角色的 wp 管理栏节点 - $wp_admin_bar->remove_node( 'string' );

Removing wp admin bar nodes for specific roles - $wp_admin_bar->remove_node( 'string' );

我正在尝试从 wp 管理栏(显示在前端)中删除特定角色(商店经理)的节点。我成功删除了节点,但不幸的是它正在对所有角色类型执行此操作。

我引用的是:https://developer.wordpress.org/reference/classes/wp_admin_bar/remove_node/

我能够删除整个管理栏,但客户希望可以轻松地将某些内容移入和移出后端。我不希望他们访问所有节点。

我用过滤器把所有的条都去掉了(这不是他们想要的)。我尝试将“if ( current_user_can( 'shop_admin' ) ){”与上面的节点代码一起使用,但不断出现错误。

//Hide Admin bar front-end

add_filter( 'show_admin_bar', function( $show ) {
    if ( current_user_can( 'shop_admin' ) ) {
        return false;
    }
    return $show;
} );

所以我是这样的:

 //Remove WP-Admin front-end Nodes

/**
 * Remove WP logo and comments from the Toolbar.
 *
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin Bar instance.
 */
function wpdocs_remove_nodes( $wp_admin_bar ) {
    // Remove items from the menu bar.
    add_filter( 'show_admin_bar', function( $show ) {
    if ( current_user_can( 'shop_admin' ) ) {
        $wp_admin_bar->remove_node( 'wp-logo' );  
        $wp_admin_bar->remove_node( 'comments' );
        $wp_admin_bar->remove_node( 'new-content' );
        $wp_admin_bar->remove_node( 'edit' );
        $wp_admin_bar->remove_node( 'litespeed-menu' );
        $wp_admin_bar->remove_node( 'theme-dashboard' );
        $wp_admin_bar->remove_node( 'customize' );
        $wp_admin_bar->remove_node( 'new_draft' );
        $wp_admin_bar->remove_node( 'updates' );
    }
        return $show;
}
add_action( 'admin_bar_menu', 'wpdocs_remove_nodes', 999 );

错误:语法错误,意外 'add_action' (T_STRING),应为 ')'

感谢您的宝贵时间!

我认为您在这里不需要额外的过滤器 show_admin_bar,所以我更新了函数以不使用它。这也是导致您的错误的原因,因为它没有正确关闭:

<?php
/**
 * Remove WP logo and comments from the Toolbar.
 *
 * @param WP_Admin_Bar $wp_admin_bar WP_Admin Bar instance.
 */
function wpdocs_remove_nodes($wp_admin_bar)
{
  // Remove items from the menu bar.
  if (current_user_can('shop_admin')) {
    $wp_admin_bar->remove_node('wp-logo');
    $wp_admin_bar->remove_node('comments');
    $wp_admin_bar->remove_node('new-content');
    $wp_admin_bar->remove_node('edit');
    $wp_admin_bar->remove_node('litespeed-menu');
    $wp_admin_bar->remove_node('theme-dashboard');
    $wp_admin_bar->remove_node('customize');
    $wp_admin_bar->remove_node('new_draft');
    $wp_admin_bar->remove_node('updates');
  }
}
add_action('admin_bar_menu', 'wpdocs_remove_nodes', 999);

对于那些希望使用 USER ID 而不是 ROLES 的人。这在我正在建设的几个网站上对我有用,但我不确定是否 100% 完美。查找 ID, array(20158,35,15558) 并从您网站的用户部分将数字更改为您自己的数字。

寻找节点: 如果您要查找自定义菜单项,可以使用浏览器“检查器”并搜索 (CSS) ID 的“wp-admin-bar-”。

找到菜单项的 (CSS) ID,然后从下面的列表中找到 add/replace。找到后不要忘记删除“wp-admin-bar-”。

所以“wp-admin-bar-MYTHINGY”现在将是“MYTHINGYhttps://developer.wordpress.org/reference/classes/wp_admin_bar/remove_node/

P.S。如果我遗漏了什么,欢迎对代码进行更改。

// Remove frontend admin bar items
function remove_admin_bar_items($wp_admin_bar) {

   if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if (in_array($current_user->ID, array(20158,35,15558))) { //ID's of users

    
        // Plugin related admin-bar items
        $wp_admin_bar->remove_node('blocksy_preview_hooks');
        // WordPress Core Items 
        $wp_admin_bar->remove_node('updates');
        $wp_admin_bar->remove_node('comments');
        $wp_admin_bar->remove_node('new-content');
        $wp_admin_bar->remove_node('wp-logo');
        //$wp_admin_bar->remove_node('site-name');
        //$wp_admin_bar->remove_node('my-account');
        $wp_admin_bar->remove_node('search');
        $wp_admin_bar->remove_node('customize');
        }
    }
}
add_action('admin_bar_menu', 'remove_admin_bar_items', 999);