0

I'm struggling with this one. I am trying to hide 2 user profile fields for the current user. Only the admin or user with "edit_users" capability should be able to view/edit these fields.

Code:

function modify_contact_methods($profile_fields) {

    // Field addition and removal will be done here
    // Add new fields
    $profile_fields['company_name'] = 'Company Name';
    $profile_fields['company_id'] = 'Company ID';

    if(current_user_can('edit_users')) {
        return $profile_fields;
    }
}
add_filter('user_contactmethods', 'modify_contact_methods');

Now it displays fine when the user is an admin, but if the user doesn't have the 'edit_users' capability, this error shows on the page between the other contact fields:

Warning: Invalid argument supplied for foreach() in /home/xxxxxxxxxxx/xxxxxxxx/wp-admin/user-edit.php on line 421

How can I correctly code it so it doesn't come up with this error? Any ideas? Any suggestion/code example is appreciated.

0

This filter needs to return something. Try this

function modify_contact_methods($profile_fields) {
    if(current_user_can('edit_users')) {
        // Field addition and removal will be done here
        // Add new fields
        $profile_fields['company_name'] = 'Company Name';
        $profile_fields['company_id'] = 'Company ID';
    }

    return $profile_fields;
}
add_filter('user_contactmethods', 'modify_contact_methods');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.