How to hide user profile fields based on the role of the viewed user?

I want to hide some default fields of the backend user profile page based on the role of the user whose profile is being

I want to hide some default fields of the backend user profile page based on the role of the user whose profile is being viewed (by the admin in this case).

I used the simple CSS way to achieve that (here hiding the Bio box):

add_action('admin_head', 'my_custom_admin_css');

function my_custom_admin_css() {
  echo '<style>

   .user-description-wrap {
       display: none;
    }

   </style>
}

but that obviously applies to ALL user roles.

Is there a way to apply this conditionally based on the role of the user whose profile is being viewed?

I want to hide some default fields of the backend user profile page based on the role of the user whose profile is being viewed (by the admin in this case).

I used the simple CSS way to achieve that (here hiding the Bio box):

add_action('admin_head', 'my_custom_admin_css');

function my_custom_admin_css() {
  echo '<style>

   .user-description-wrap {
       display: none;
    }

   </style>
}

but that obviously applies to ALL user roles.

Is there a way to apply this conditionally based on the role of the user whose profile is being viewed?

Share Improve this question asked Dec 16, 2019 at 11:14 Chris OsiakChris Osiak 117 bronze badges 1
  • Perhaps you start taking a look here current_user_can – Charles Commented Dec 16, 2019 at 11:35
Add a comment  | 

1 Answer 1

Reset to default 1

current_user_can() checks the current user's specified capability. See Roles and Capabilities.

Also, CSS just visually hides the section, but does not remove it from page HTML. The following code checks the capability and completely removes the section.

<?php
// For example, 
// any user other than an Admin of single site installation,
// but not Multisite Admin and not SuperAdmin:
if ( ! current_user_can( 'delete_users' ) ) {

    add_action( 'admin_head', 'my_profile_admin_buffer_start' );
    add_action( 'admin_footer', 'my_profile_admin_buffer_end' );

}

function my_remove_about_section( $buffer ) {

    // get section header and everything until the next section
    $about_section = '~<h2>About Yourself</h2>.+?/table>~s';
    // replace it with empty string
    $buffer = preg_replace( $about_section, '', $buffer, 1 );

    return $buffer;
}

function my_profile_admin_buffer_start() {
    ob_start( 'my_remove_about_section' );
}

function my_profile_admin_buffer_end() {
    ob_end_flush();
}

The whole idea is taken from my old dead pre-WordPress-5 project and is not tested.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744899726a4599906.html

相关推荐

  • How to hide user profile fields based on the role of the viewed user?

    I want to hide some default fields of the backend user profile page based on the role of the user whose profile is being

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信