Add and/or remove WordPress user fields

Posted on Leave a comment
5/5 - (123 votes)

Simplicity should be a key goal in design and unnecessary complexity should be avoided. Therefore, Keep It Simple, Stupid. Or, minimalism!

Its certainly cause for celebration that — along with the overall, welcome minimalism visited upon the WordPress admin UI — as of the recent version of WordPress, rather antiquated User fields are no more. Navigate to Users > Your Profile and, unless explicitly added by a (possibly antiquated) theme or plugin, you see a welcome few user contact fields. Email, website, bio, etc. Strictly the basics.

Time comes though, when you want to add a reference to Skype or to Twitter or a link to Dribble or Facebook, etc. As usual, this is rather straightforward.

With the user_contactmethods filter, we can add or remove the available contact methods on a User’s profile page.

Defining the function

As usual, first we define a function. Lets call it butter_modified_fields(). This function will accept one argument $contact_methods which will be an associative array.

function butter_modified_fields( $contact_methods ){
// define the new/additional User fields here
}

After we’ve defined the function, we’ll then need to hook into the user_contactmethods filter. We’ll have this function make the contact_methods accept our new User fields form ids as the keys and the human-readable form labels as the values. Sounds more way complicated that it looks. Therefore, look:

 

function butter_modified_fields( $contact_methods ){

	$contact_methods['skype'] = __('Skype Username’, ‘butter’); 
	$contact_methods['twitter'] = __('Twitter Username', ‘butter’); 
	$contact_methods['dribbble'] = __('Dribbble Username', ‘butter’); 
	$contact_methods['facebook'] = __(‘Full FB URL', ‘butter’); 

	return $contact_methods;
}

add_filter('user_contactmethods', 'butter_modified_fields');

Removing the old fields

Not too long ago, as mentioned, you had user fields for stuff like ‘aim’ and ‘jabber’. It’s not clear that there are any more teenagers from the late 90s left around still using aim. Nobody actually even knows what jabber is.

If you’re still looking at these fields in your User > Your profile views, chances are you need to promptly upgrade your installation to the latest WordPress version. If that’s an impossibility for you, fret not young Padawan, all you need to do is to adjust the above function. Having already added SkypeTwitterDribbble and Facebook fields, you can remove aim and jabber as follows:

function butter_modified_fields( $contact_methods ){

	// lines where you added Skype, twitter, dribble, etc above
	. . .

	// Unset fields you don’t need
	unset($contact_methods['aim']);
	unset($contact_methods['jabber']);

	return $contact_methods;
}

add_filter('user_contactmethods', 'butter_modified_fields');

With that, your user profile UI is as svelte as you dreamt it to be. Be careful though, while reductive methods might work great in art class e.g. when working with charcoal on paper, in WordPress, removing core-defined functions and features isn’t a particularly good thing to do.

If the very sight of the fields makes you retch uncontrollably, by all means implement the removal of the contact method. Otherwise, its just as easy or arguably easier to altogether ignore the fields and not fill anything in them.

In fact, if you plan to distribute your theme through the top few marketplaces, avoid removing or crippling WordPress functionally in any way. As much as you morally (or otherwise) might disagree, your opinion won’t matter. Your crippled theme submission to these marketplaces will be categorically and summarily rejected. If you’re theming purely for fun and not profit, definitely experiment.

Showing the fields

Now that we’ve defined the function and understood that a more additive approach is far better than a reductive approach, we move on to using the function. On a multi-contributor magazine or newspaper site, for example, on each author’s profile page, you’d simply make a call for the relevant field like so:

<?php echo the_author_meta('twitter'); ?>

Depending on what markup you use, you might want to wrap that in a conditional to first check that the field isn’t empty. Like so:

<?php if ( get_the_author_meta('twitter') ) :  ?>
	<?php the_author_meta('twitter'); ?>
<?php endif; ?>

Make that $#!% pluggable!!!

Like that catchy yet epically annoying song that’s routinely stuck in your head at the most inopportune of times, hopefully this is doing the same. If you take nothing away from this guide, the tl;dr is simply that:

there’s a function for that!

and once you find that function:

make that $#!% pluggable!

While you might think no one would have interest in filling in their jabber and/or aim profiles, your theme’s end user (and more likely their client [2 degrees of Kevin Bacon]) might very well have use for them. To that end, don’t cripple WordPress for no debatable reason but if you must make what you do easily reversible by the next person that hopefully has better sense.
Say it out loud: MAKE THE FUNCTION PLUGGABLE!

Having already defined the function, naturally, this is simple. All we have to do is make sure to wrap it in a ( ! function_exists() ). So, the butter_modified_fields() functions that we defined in functions.php would look like the so:

// be nice to the children (make it pluggable)
if ( ! function_exists( 'butter_modified_fields' ) ) :

  function butter_modified_fields( $contact_methods ){

    $contact_methods['skype'] = __('Skype Username’, ‘butter’); 
    $contact_methods['twitter'] = __('Twitter Username', ‘butter’); 
    $contact_methods['dribbble'] = __('Dribbble Username', ‘butter’); 
    $contact_methods['facebook'] = __(‘Full FB URL', ‘butter’);

    // Unset fields you don’t need
    // you CAN do this but maybe think about it before you do
    unset($contact_methods['aim']);
    unset($contact_methods['jabber']);

    return $contact_methods;
  }

  add_filter('user_contactmethods', 'butter_modified_fields');

endif;

And that’s it for that. The next section covers how to include these user contact methods in a Team/Contributors page.

Leave a Reply

Your email address will not be published. Required fields are marked *