Home  >  Article  >  Backend Development  >  Quick Tip: Add additional contact details to your user profile

Quick Tip: Add additional contact details to your user profile

WBOY
WBOYOriginal
2023-09-02 10:33:06995browse

If you search for "adding extra fields to WordPress user profiles" you will find various coding examples involving adding extra inputs to user profile pages so that you can capture additional user information. But if you just want to extend the default contact section, there's an easier way.


user_contactmethods filter

The

user_contactmethods filter allows you to set and unset contact information fields on user profile pages. The benefit of using this method is that WordPress takes care of creating and updating the fields.

Let's add Twitter and Facebook information fields. Put this in your functions.php file:

add_filter('user_contactmethods', 'my_user_contactmethods');
             
function my_user_contactmethods($user_contactmethods){

  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';

  return $user_contactmethods;
}

This is what you will get:

Quick Tip: Add additional contact details to your user profile

If you want to remove certain fields, just unset them from the array:

function my_user_contactmethods($user_contactmethods){

  unset($user_contactmethods['yim']);
  unset($user_contactmethods['aim']);
  unset($user_contactmethods['jabber']);

  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';

  return $user_contactmethods;
}

Quick Tip: Add additional contact details to your user profile

To display the user's information, just use the get_user_meta function.

echo get_user_meta(1, 'twitter', true);

This will display the Twitter username of the user with ID 1. The true parameter causes the data to be returned as a single value rather than an array.

That’s all!

The above is the detailed content of Quick Tip: Add additional contact details to your user profile. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn