To add custom fields to the user profile in WordPress, you can use the user_contactmethods
filter in your theme’s functions.php
file. The following code snippet will add a new field for the user’s Twitter handle:
function wpsnippets_add_twitter_field( $user_contactmethods ) {
$user_contactmethods[ 'twitter' ] = __( 'Twitter Handle', 'wpsnippets' );
return $user_contactmethods;
}
add_filter('user_contactmethods', 'wpsnippets_add_twitter_field' );
This code adds a new field to the user profile by adding a new key-value pair to the $user_contactmethods
array. The key twitter
is the name of the new field, and the value Twitter Handle
is the label that will be displayed next to the field. You can modify these values to match the field you want to add.