Wednesday, November 11, 2020

Revamp User Profiles on the Admin Panel with Custom Contact Fields in WordPress using the "user_contactmethods" Filter

With the user_contactmethods filter, you can easily enhance the contact information fields available to your WordPress users. This filter allows you to edit the available contact methods on a user's profile page, including adding and removing fields. By customizing the contact fields, you can provide a more personalized experience for your users.

To get started, simply use the user_contactmethods filter and pass an associative array of the contact fields you want to add or remove. For example, you can add fields for Skype and Twitter usernames, or remove fields for user's Aim.

Here's an example code snippet that demonstrates how to use the user_contactmethods filter:

Parameters

  1. array $user_contact : an associative array
  2. WP_User $user: WP_User object or null if none was provided.
  3. Code Example Below: 
add_filter( 'user_contactmethods', 'modify_user_contact_methods' );
function modify_user_contact_methods( $user_contacts ) {
    // Add user contact methods
    $user_contacts['skype']  = __( 'Skype Username', 'text-domain' ); 
    $user_contacts['twitter'] = __( 'Twitter Username', 'text-domain' ); 

    // Remove user contact methods
    unset( $user_contacts['aim'] );

    return $user_contacts;
}
          

By using this filter, you can easily customize the contact fields available to your users and provide a more personalized experience on your WordPress site.


Reference

  1. https://codex.wordpress.org/Plugin_API/Filter_Reference/contactmethods 
  2. https://developer.wordpress.org/reference/functions/wp_get_user_contact_methods/