根据用户角色/功能从菜单隐藏DIV Wordpress [英] Hiding DIV from menu based on user role / capabilities Wordpress

查看:154
本文介绍了根据用户角色/功能从菜单隐藏DIV Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有2个用户角色(客户和供应商)的wordpress网站.我想从客户"菜单栏中隐藏供应商仪表板".该主题的代码在header-aside.php文件中,因此插件将无法隐藏该元素.我尝试了下面的代码,这些代码对所有人(不仅是客户)都隐藏了这一点,所以我不确定在这里有什么错.

I have a wordpress site with 2 user roles, customer and vendor. I want to hide the Vendor Dashboard from the menu bar from Customers. The code for this theme is in the header-aside.php file so a plugin won't work to hide this element. I have tried the following code which hides this from everyone, not just customers, so I'm not sure what I have wrong here.

     <?php if (current_user_can(‘read’)) { ?>
        <div class="dashboard-icon">
            <a id="header-button" href="/creator-dashboard/" class="header-button boss-tooltip" data-tooltip="<?php _e( 'Creator Dashboard', 'onesocial' ); ?>"><i class="fas fa-tachometer-alt"></i></a>
        </div>
    <?php } ?>

请注意,由于我无法显示它,因此我可以在其中读取"作为测试.但是供应商的实际能力是"edit_products"

Note I have the capability 'read' in there as a test since I am unable to get it to show. But the actual capability a vendor has is 'edit_products'

推荐答案

您可以在 wp_get_current_user()返回. 因此,您可以通过该代码仅向供应商显示仪表板:

You can check the user roles inside the WP_User object, which is returned by the function wp_get_current_user (). So you may show Dashboard only for Vendor by that code:

<?php 
$user = wp_get_current_user();
if ( in_array( 'vendor', $user->roles ) ) {
?>
    <!-- Any HTML what you need to hide from "Customers" and show for "Vendor" -->
    <div>Vendor Dashboard</div>
<?php 
}
?>

这篇关于根据用户角色/功能从菜单隐藏DIV Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆