admin管理员组

文章数量:1025259

I am trying to add a custom field called Company to all site admins on my network. And disable this field for every role which hasn't got the capability to create_users.

This is what I have come up with, but I cannot make it work:

function modify_contact_methods($profile_fields) {
  $user = $_GET['user_id'];
  if (!user_can($user, 'create_users')) {
       return;
  }
  $profile_fields['company'] = 'Company';
  return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

So what I ultimately want is for it to be hidden from user-edit.php and profile.php, on user-edit.php it should be possible to get the user id from the URL, but on profile.php I don't know. I cannot make the above code work however.

I am trying to add a custom field called Company to all site admins on my network. And disable this field for every role which hasn't got the capability to create_users.

This is what I have come up with, but I cannot make it work:

function modify_contact_methods($profile_fields) {
  $user = $_GET['user_id'];
  if (!user_can($user, 'create_users')) {
       return;
  }
  $profile_fields['company'] = 'Company';
  return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

So what I ultimately want is for it to be hidden from user-edit.php and profile.php, on user-edit.php it should be possible to get the user id from the URL, but on profile.php I don't know. I cannot make the above code work however.

Share Improve this question asked Mar 5, 2019 at 14:44 joq3joq3 3813 silver badges21 bronze badges 3
  • You should probably return $profile_fields in the no-permissions case (or just only add it if the user has permissions). But I don't think that's going to be enough. I'd guess you'd need to add the extra field generally and decide at display time whether to render it or not, but I can't think how to do that off the top of my head. – Rup Commented Mar 5, 2019 at 14:59
  • have you tried with is_admin() ? – Gregory Commented Mar 5, 2019 at 16:16
  • Yes, but that doesn't work! And it would not remove the field from when the admin is editing a users profile. – joq3 Commented Mar 5, 2019 at 17:26
Add a comment  | 

1 Answer 1

Reset to default 0

user_contactmethods filter hook passes two parameters to the registered functions. The second parameter is the WP_User object, with the help of which you can check roles and caps of the edited user.

add_filter( 'user_contactmethods', 'se330743_user_contact_methods', 20, 2 );

function se330743_user_contact_methods( $user_contact, $user )
{
    // --- fields for admins ---
    if ( !in_array('administrator', (array)$user->roles) )
        return $user_contact;

    // --- fields for users with cap 'create_users' ---
    // if ( !user_can($user, 'create_users') )
    // --- fields for admin---
    // if ( !in_array('administrator', (array)$user->roles) ||  !user_can($user, 'create_users') )

    $user_contact['company'] = 'Company';
    return $user_contact;
}

I am trying to add a custom field called Company to all site admins on my network. And disable this field for every role which hasn't got the capability to create_users.

This is what I have come up with, but I cannot make it work:

function modify_contact_methods($profile_fields) {
  $user = $_GET['user_id'];
  if (!user_can($user, 'create_users')) {
       return;
  }
  $profile_fields['company'] = 'Company';
  return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

So what I ultimately want is for it to be hidden from user-edit.php and profile.php, on user-edit.php it should be possible to get the user id from the URL, but on profile.php I don't know. I cannot make the above code work however.

I am trying to add a custom field called Company to all site admins on my network. And disable this field for every role which hasn't got the capability to create_users.

This is what I have come up with, but I cannot make it work:

function modify_contact_methods($profile_fields) {
  $user = $_GET['user_id'];
  if (!user_can($user, 'create_users')) {
       return;
  }
  $profile_fields['company'] = 'Company';
  return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

So what I ultimately want is for it to be hidden from user-edit.php and profile.php, on user-edit.php it should be possible to get the user id from the URL, but on profile.php I don't know. I cannot make the above code work however.

Share Improve this question asked Mar 5, 2019 at 14:44 joq3joq3 3813 silver badges21 bronze badges 3
  • You should probably return $profile_fields in the no-permissions case (or just only add it if the user has permissions). But I don't think that's going to be enough. I'd guess you'd need to add the extra field generally and decide at display time whether to render it or not, but I can't think how to do that off the top of my head. – Rup Commented Mar 5, 2019 at 14:59
  • have you tried with is_admin() ? – Gregory Commented Mar 5, 2019 at 16:16
  • Yes, but that doesn't work! And it would not remove the field from when the admin is editing a users profile. – joq3 Commented Mar 5, 2019 at 17:26
Add a comment  | 

1 Answer 1

Reset to default 0

user_contactmethods filter hook passes two parameters to the registered functions. The second parameter is the WP_User object, with the help of which you can check roles and caps of the edited user.

add_filter( 'user_contactmethods', 'se330743_user_contact_methods', 20, 2 );

function se330743_user_contact_methods( $user_contact, $user )
{
    // --- fields for admins ---
    if ( !in_array('administrator', (array)$user->roles) )
        return $user_contact;

    // --- fields for users with cap 'create_users' ---
    // if ( !user_can($user, 'create_users') )
    // --- fields for admin---
    // if ( !in_array('administrator', (array)$user->roles) ||  !user_can($user, 'create_users') )

    $user_contact['company'] = 'Company';
    return $user_contact;
}

本文标签: customizationAdd custom profile field only for site admins