【问题标题】:Hide fields based on user role根据用户角色隐藏字段
【发布时间】:2018-07-19 04:53:33
【问题描述】:

如何使用 CSS 为特定用户隐藏字段?
像这样的:

if( $role == 'subscriber' ) {    ?>
<style type="text/css">
    .user-area .user-profile-dropdown{
        display: none !important;
    }
</style>

我使用了这段代码,但它不起作用:

function hide_fields_nonusers(){
if( $role == 'subscriber' ) {    ?>
<style type="text/css">
    .user-area .user-profile-dropdown{
        display: none !important;
    }
</style>
}
add_action('admin_head', 'hide_fields_nonusers');

【问题讨论】:

  • 第一:怎么没用?第二:您是否试图仅在客户端隐藏数据?
  • 是的。它什么也没做!
  • 如果有人使用浏览器上的开发者控制台读取隐藏数据会不会成为问题?

标签: php css wordpress


【解决方案1】:

您没有关闭 HTML 输出:

function hide_fields_nonusers(){
if( $role == 'subscriber' ) {    ?>
<style type="text/css">
  .user-area .user-profile-dropdown{
     display: none !important;
 }
</style>
<?php }

【讨论】:

  • 什么意思?您输入的代码与我的代码相同。什么是正确的代码?
  • 我修复了语法错误并测试了它,它不起作用
【解决方案2】:

想通了:

if( current_user_can('subscriber')) {   
?>
<style type="text/css">
.user-area .user-profile-dropdown{
 display: none !important;
 }
</style>
<?php 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 2017-07-22
    • 2019-05-17
    • 1970-01-01
    • 2011-02-25
    • 2012-06-20
    • 2021-12-01
    • 2012-12-14
    相关资源
    最近更新 更多