Add GitHub or Any Contact Method to WordPress

add github or any contact method to wordpress user page thumbnail

Adding social contact methods to a WordPress user account admin panel doesn’t need to be difficult. If you have been around WordPress for a little while you may remember at one time there was more contact method fields, it even had some obscure ones like jabber and aim, but now there is just the bare minimum – email and website, Not even the more popular socialist sites, like Facebook and Twitter. And if you think about it it makes sense, even if WordPress could list all the different platforms, would you really want every site known, in your admin panel? Doubtful, instead WordPress leaves it up to us to

decide which ones we want. WordPress provides a filter that is easy to use and leaves your dashboard with only the accounts that are important to the site owner. I will try to show how this function can be used to get the social media links that you need. So inside your child theme’s function.php or included in there, or better yet in a custom plugin since you probably don’t want to lose these fields even if you were to change themes in the future.

The Defualt WordPress Contact Methods Admin Panel
The Defualt WordPress Contact Methods Admin Panel

 

Lets create a function that I will call lems_contact_method_fields(), it can be called what ever you like but must be unique and should be descriptive of what we are doing, the lems_ prefix is just a namespace to help ensure the uniqueness of the function’s name. You are free to change if to anything you like. (At the end we will wrap this in a check so that if for some reason the function is not available it won’t throw an error if called).

function lems_contact_method_fields() { 

}

To this function we want to pass the user_contactmethods array, lets use a variable that we name $contact_methods  (we pass this inside the parentheses).

function lems_contact_method_fields( $contact_methods ) {

}

Lets add a field for GitHub to the $contact_methods[] array the key which is the string 'github' while the string on the right is the value for the label that will show on the admin screen.

function lems_contact_method_fields( $contact_methods ) {
    $contact_methods['github'] = 'GitHub username';
}

And lets do another one, how about LBRY.

function lems_contact_method_fields( $contact_methods ) {
    $contact_methods['github'] = 'GitHub username';
    $contact_methods['lbry']   = 'LBRY channel name';
 }

We need to be sure to return the $contact_methods array to make it available.

function lems_contact_method_fields( $contact_methods ) {
    $contact_methods['github'] = 'GitHub username';
    $contact_methods['lbry']   = 'LBRY channel name'];
    
    return $contact_methods;
}

Now that we have our function defined, we will hook into the user_contactmethods filter.

add_filter( 'user_contactmethods', 'lems_contact_method_fields' );

We have now added the new contact methods to the array. They are saved in the database and can be used by the same functions we would normally use, for example.

GitHub Added to the default contact methods admin panel
This is how our Admin panel with our new contact methods fields. All the saving and sanitization is handled by WordPress using this way of adding Contact Methods.

 

To use this on a website, we might use one of the WordPress functions such as the_author_meta(), or get_the_author_meta() to display the author of a post’s social media accounts. To display the GitHub account profile link for the author of a post we could

if ( get_the_author_meta('github') ) {
  the_author_meta('github');
}

This will check that the field is not empty before echoing the value.

If for some reason you have input fields in your admin that you don’t use and don’t want to show. Perhaps you have an older version of WordPress that you can’t upgrade for some reason, it is worth noting that you can remove the input fields. To accomplish this we would use unset() like this

function lems_contact_method_fields( $contact_methods ) {
    unset( $contact_methods['jabber'] );
    return $contact_methods;
}

So lets add a check that the function exists before calling it so we don’t set off any errors calling a function that doesn’t exist, and look at the completed function, I am going to comment out the unset because I don’t needed it, but will be there as a reference.

if ( ! function_exists( 'lems_contact_method_fields' ) ) :
    function lems_contact_method_fields( $contact_methods ) {
        $contact_methods['github'] = 'GitHub username';
        $contact_methods['lbry']   = 'LBRY channel name';

        //unset( $contact_methods['jabber] );

        return $contact_methods;
    }
add_filter( 'user_contactmethods', 'lems_contact_method_fields' );
endif;

And there we have it, an easy way to add any contact method input fields to the user admin panel.

GitHub and LBRY Added to the default contact methods admin panel
Admin Panel with both of the new fields.

 

 

To show use the new contact methods in a template or elsewhere in a theme, we would use the_author_meta('github'); or get_the_author_meta('github');  if we wanted the author of a post. If we want user information for example the admin account – we could use get_userdata('1', 'github'); where the '1' is the user_id. This would all depend on the use case of course, to determine which would be used.

Brave Browser Ad