user_contactmethods

Home » Hooks » user_contactmethods

The user_contactmethods hook is a WordPress filter that allows developers to add custom contact methods to user profiles in the WordPress dashboard. By default, WordPress provides users with contact methods such as email, website, AIM, Yahoo IM, Jabber/Google Talk, and biographical info. However, with the user_contactmethods hook, developers can add new contact methods to user profiles, customize existing contact methods, or remove contact methods altogether.

This hook is particularly useful for WordPress developers who are creating custom themes or plugins and want to add custom fields to user profiles. For example, a plugin developer might want to add a custom "Twitter" field to user profiles, allowing users to enter their Twitter handle and display it publicly on their author pages. Alternatively, a theme developer might want to remove certain default contact methods that don’t apply to their website or brand.

Here’s an example of how to use the user_contactmethods hook in a plugin:

function myplugin_add_user_contact_methods( $user_contactmethods ) {
    // Add a custom Twitter field
    $user_contactmethods['twitter'] = __( 'Twitter Handle', 'myplugin' );
    return $user_contactmethods;
}
add_filter( 'user_contactmethods', 'myplugin_add_user_contact_methods' );

In this example, we’re using the add_filter() function to add a custom "Twitter" field to user profiles. The callback function, myplugin_add_user_contact_methods(), takes a single parameter ($user_contactmethods) which is an array of the default contact methods provided by WordPress. We then add our custom "Twitter" field to this array and return it to the filter. The __() function is used to provide a translation-ready string for our custom field label. Finally, we attach our callback function to the user_contactmethods filter using add_filter().

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.