I’m adding a custom profile field to users of a specific role, like this :
function add_custom_profile_fields( $fields ) {
// get current user ID
$user = new WP_User( $_GET['user_id'] );
// get current user role
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
// filter roles
if ($role == "paying_member"){
$fields['paypal_account'] = 'Paypal account';
}
}
}
return $fields;
}
add_filter('user_contactmethods','add_custom_profile_fields',10,1);
The problem is, the field’s value doesn’t get saved. When I login as admin an I edit a user’s profile. It somehow has to do with the fact that I’m filtering by user role, because when I remove that part, the values get saved perfectly.
EDIT : I think maybe the whole method is wrong, I’m going to try this instead.
Ok, I was doing it wrong, here’s a working solution, based on Justin Tadlock’s tutorial.
The main addition to his code, is this line of code:
Which displays the custom fields only for users with the role of “paying_member” and admins.
You can skip the looping over roles and use user_can
The function takes either a capability or a role as param.
Might also be worth checking into current_user_can to skip the user lookup.
The problem is that $_GET[‘user_id’] is not populated. You want to pull in the current user variable. Try this out: