Add And Remove WordPress User Profile Fields And Display Them Within Your Theme

I recently had to remove and add fields to the user profile of  WordPress for one of my clients. The instructions were simple and I had to remove quite a bit of fields which they specified and also had to add a way for the users to upload an image.

So today I will show you two types of ways to remove fields, plus how to add your upload functionality already build into WordPress.  I will also show you how to retrieve and display your user information.

Here is the code to remove first couple of fields

//hides the personal options
function hide_personal_options(){
echo "\n" . '<script type="text/javascript">jQuery(document).ready(function($) {
$(\'form#your-profile > h3:first\').hide();
$(\'form#your-profile > table:first\').hide();
$(\'form#your-profile\').show();

$(\'label[for=url], input#url\').hide();
});

</script>' . "\n";
}
add_action('admin_head','hide_personal_options');

Next we have to unset some fields

	//remove default fields
    function hide_profile_fields( $contactmethods ) {
    unset($contactmethods['aim']);
    unset($contactmethods['jabber']);
    unset($contactmethods['yim']);
    return $contactmethods;
    }
	add_filter('user_contactmethods','hide_profile_fields',10,1);

Okay, now that we have removed our fields we need to add an upload field and two extra fields.

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

<h3>Extra profile information</h3>

<table class="form-table">

<tr>
<th><label for="image">Profile Image</label></th>

<td>
<img src="<?php echo esc_attr( get_the_author_meta( 'image', $user->ID ) ); ?>" style="height:50px;">
<input type="text" name="image" id="image" value="<?php echo esc_attr( get_the_author_meta( 'image', $user->ID ) ); ?>" class="regular-text" /><input type='button' class="button-primary" value="Upload Image" id="uploadimage"/><br />
<span class="description">Please upload your image for your profile.</span>
</td>
</tr>

<tr>
<th><label for="image">Your Age</label></th>

<td>
<input type="text" name="age" id="age" value="<?php echo esc_attr( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" />
<span class="description">Please type your age.</span>
</td>
</tr>

<tr>
<th><label for="image">Interests</label></th>

<td>
<input type="text" name="interest" id="interest" value="<?php echo esc_attr( get_the_author_meta( 'interest', $user->ID ) ); ?>" class="regular-text" />
<span class="description">Type your Interests here.</span>
</td>
</tr>

</table>
<?php }

Our upload functionality won’t work yet, so here is the code to make it work.

function zkr_profile_upload_js() {
?><script type="text/javascript">
jQuery(document).ready(function() {
jQuery(document).find("input[id^='uploadimage']").live('click', function(){
//var num = this.id.split('-')[1];
formfield = jQuery('#image').attr('name');
tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');

window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#image').val(imgurl);
tb_remove();
}

return false;
});
});
</script>
<?php
}
add_action('admin_head','zkr_profile_upload_js');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_style('thickbox'); //thickbox styles css

Now we need to have a way to save our field data.

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return false;

update_usermeta( $user_id, 'image', $_POST['image'] );
update_usermeta( $user_id, 'age', $_POST['age'] );
update_usermeta( $user_id, 'interest', $_POST['interest'] );
}

That’s it your new fields are added and you should be able to save them now. Next we need to be able to display our fields. You can put the following code anywhere you would like to display your data.


<img src="<?php echo the_author_meta('image'); ?>" style="height:50px; width:50px;" />

Age: <?php echo the_author_meta('age'); ?>

Interests: <?php echo the_author_meta('interest'); ?>

I’ve written the main user profile code in to a plugin for you so you can download it and see how it works for yourself.

Hope you enjoyed this tutorial and I just want to wish you all happy holidays and a merry Christmas from TeachingYou.net

You have to agree to download the code.

Simply enter your email address and the download link will be sent right to your inbox.

Related Posts:

  • rss
  • email
  • rss
  • email
I'm a full time PHP developer and I just love all things web related. If you need help I'm your guy.
  • Sandeep816

    Thanks to this article.. :)

  • http://teachingyou.net Morné

    No Problem, hope it was helpful. :)

  • Ayaz Mahmood

    Very helpful and good explanation.

  • Vahid

    Hi,

    Thank you so much. It was so helpful.

    There is just one thing. I can not display the fields on the author.php page. Could you please help?

  • http://teachingyou.net Morné

    Hi Vahid,

    You should be able to add the following code somewhere in author.php.

    <img src="ID ) ); ?>” alt=”" />

    Hope that helps you out. Let me know if it worked for you?

    Thanks for the comment.

  • Vahid

    Hi Morné,

    Thank you. I Made it work finally. Your codes helped me a lot and saved me a lot of time. Thank you.

  • http://teachingyou.net Morné

    Hi Vahid,

    Glad I could help you out. Please like the article and share it with someone you think might like it :)

    Also keep an eye out for new articles via the RSS feed.

    Once again thanks for the comments.

  • Andreafanfa

    Does the image upload works for you? Mine doesn’t. It just shows no action if you click on it…

  • http://teachingyou.net Morné

    Hi Andrea,

    Yes it does work for me. Did you try to code it yourself or did you download the plugin code?

  • Andrea Amado

    congratulations, works like charm.
    thanks ;)

  • http://twitter.com/AlessandroCardy Alessandro Cardinali

    Hello. I have to upload more then 1 image… how can I do for it?

  • http://teachingyou.net Morné

    Hi Alessandro,

    You can already upload multiple images with this code. You just need a way to display more then one. But I would suggest trying to add additional upload fields and meta. I can’t think of a solution now from the top of my head.

    Maybe look at implementing an alternative jQuery up loader and then store image data in the database and call them from there for each user.

    Hope that gives you an idea.

    Thanks for the comment ;)

  • http://www.facebook.com/profile.php?id=1661765997 Rocco Panetta

    Hi Morné! :)
    Thanks very very much for this helpful tutorial!
    It’s very clear and it works like a charm! :)
    I’d like some new fields to be mandatory.. a kind of “non-empty” option… Can you help me, please??
    Thank you in advance,
    Rocco :)

  • http://teachingyou.net Morné

    Hi Rocco
    You would need to explain a bit more so that I can help you out. Give me a better idea of what you would like to achieve?

  • http://www.facebook.com/profile.php?id=1661765997 Rocco Panetta

    I’d like that some fields “must” be filled:
    if these “mandatory” fields are filled, the profile updating will be succesful;
    on the other hand, if these “mandatory” fields are empty, the updating process will give an error message, i.e. on top of the profile page (mentioning the reason of the error), and the modified fields won’t be saved as far as the “mandatory” fields are not filled.
    Thank you for your patience.. :)

  • http://teachingyou.net Morné

    Hi Rocco,

    Within the php code you can do some php validation to check for empty $_POST data.

    So you do something like this:

    if(!empty($_POST['image'])){

    update_usermeta( $user_id, ‘image’, $_POST['image'] );

    }else{
    echo “Image field is required!”;
    }

    That should work for basic server side validation. If need to go the
    client side validation route,
    I suggest you look into jQuery validation

    You will still need to do the server side validation as a backup.

  • http://www.facebook.com/profile.php?id=1661765997 Rocco Panetta

    Oh, it was very easy to implement the “non empty checking” for the custum fields I created. Just chech out how it works for the already existent fields in wp-adminincludesuser.php and add the code with the proper tweaks in the same file!
    Of course, Mornè, your solution works as well!
    Tnk you very much! :)

  • http://teachingyou.net Morné

    Glad you found a solution :) did not even think if checking that file. ;)

  • http://twitter.com/siteuntitled Site Untitled

    Thanks! This is great. I’m trying to adapt the image upload field into a field where users can upload their cv/resume. Everything seems to be working okay, except that after I select the file the upload field does not update and will not save.

    I changed:

    <img src="ID ) ); ?>” style=”height:50px;”>
    <input type="text" name="image" id="image" value="ID ) ); ?>” class=”regular-text” />
    Please upload your image for your profile.

    to:

    < a id="cv_link" href="ID ) ); ?>”>Download
    <input type="text" name="cv" id="cv" value="ID ) ); ?>” class=”regular-text” />
    Please upload your Curriculum vitae / Resume for your profile.

    and then:

    jQuery(document).find(“input[id^='uploadimage']“).live(‘click’, function(){
    //var num = this.id.split(‘-’)[1];
    formfield = jQuery(‘#image’).attr(‘name’);
    tb_show(”, ‘media-upload.php?type=image&TB_iframe=true’);

    window.send_to_editor = function(html) {
    imgurl = jQuery(‘img’,html).attr(‘src’);
    jQuery(‘#image’).val(imgurl);
    tb_remove();
    }

    to:

    jQuery(document).find(“input[id^='uploadcv']“).live(‘click’, function()
    {
    //var num = this.id.split(‘-’)[1];
    formfield = jQuery(‘#cv’).attr(‘name’);
    tb_show(”, ‘../media-upload.php?TB_iframe=true’);

    window.send_to_editor = function(html)
    {
    cvurl = jQuery(‘#cv_link’,html).attr(‘href’);
    jQuery(‘#cv’).val(cvurl);
    tb_remove();
    }
    return false;
    });

    Any idea what I’m doing wrong? Thanks!

  • Lopo

    can’t download the code :)

  • http://teachingyou.net Morné

    It will only work if you give a valid email, if all else fails check your spam box. The link gets mailed to you. Just tested and works on my side.

  • http://twitter.com/siteuntitled Site Untitled

    I noticed it saves the field if you type in a file location directly, but it is still not updating the field after choosing a file.

  • http://twitter.com/siteuntitled Site Untitled

    Got it! I had to change:

    imgurl = jQuery(‘img’,html).attr(‘src’);
    to
    imgurl = jQuery(‘img’,html).attr(‘src’);

    to

  • http://teachingyou.net Morné

    Hi there, Glad you figured it out. Sorry that I did not reply to you. Busy life :) Planning on writing some new articles this month so look out for them. Cheers

  • ali

    Hi, what else did u change to make it upload a cv/resume, please help me

    Thanks

  • Alena

    Hi It is exactly what I need. I received zip file from you but how I could activate/set up it? I copied this file to wp-content/plugins folder by total commander but nothing changed. Thank you for your help.

  • http://teachingyou.net Morné

    Hi Alena, You need to upload the zip file via the wordpress backend under plugins > add new.

    Or you can unzip the folder and then use filezilla or any other FTP program to upload it to your plugins folder.

    Hope that helps.

  • http://www.facebook.com/brigitte.giguere.16 Brigitte Giguère

    Hello Morné, I’ve downloaded your plugin. I’m using PremiumPress’ DirectoryPress theme. I’m getting the following error message: Warning: Cannot modify header information – headers already sent by (output started at /home/brig8108/public_html/formationsgestionnaires.com/wp-content/themes/directorypress/functions.php:19) in /home/username/public_html/formationsgestionnaires.com/wp-includes/pluggable.php on line 881

    Any idea what could be the problem? Thanks in advance, Brigitte

  • http://teachingyou.net Morné

    Hi Brigitte,

    The issue here is not the plugin. The error is pointing to your directorypress themes functions.php file on line 19.

    As I can’t see the code I suggest you look at the file and at the end of the file make sure that there is no spaces after the last closing php ?> tag.

    Also make sure that there is no whitespaces in your wp-config.php file before the first opening closing tag.

    Hope this helps. If you struggle, then please contact me through my contact page then I can help you from there and fix that file for you.

    Have nice day!

  • club22

    thank you verry much but it’s not working,it adds the extra profile fields with age interests and image upload but doesn’t show it….when someone writes a comment if i click the picture of the comment author,it sends me to a profile page where appears only the authors avatar….

    what I need to do: I need a user profile page with basic information like: name,rank(admin,subscriber etc.),age,adress(not necesarry),post count,comment count,password change,picture upload…..I want to remove the wordpress user profile page because it seems not to work(I fill every field in the edit profile but it doesn’t take effect….only the upload avatar works.) pls help me with this,I know nothing about coding,I am willing to pay u.sorry for my bad english.

  • http://teachingyou.net Morné

    Hi There,

    I can help you out. Do you have a domain where you need this setup?

  • club22

    yes I have a temporary domain club22.net63.net can u pls add me on yahoo messenger to talk faster? my id is alexandrumdc

  • http://www.facebook.com/imran.husain.3760 Imran Husain

    My functions.php file has no ?> at the end. When I tried to append the code at the end, the theme(Twenty Eleven 1.3) didn’t load and I received an error at the first line of the code. Request for more information please. Thanks.

  • http://teachingyou.net Morné

    Hi Imran,

    I suggest you download the code, as it’s already in a plugin which you can just activate and use.

    Regards.

  • http://teachingyou.net Morné

    Do you have skype? we can chat through skype it’s better, or even gtalk.

  • club22

    my skipe id is alexandru1st sorry for the late reply I had problems with my internet connection

  • club22

    my skype id is alexandru1st sorry for the late reply I had internet connection problems.

  • Nancy Boyle

    Very helpful! Thank you. I have used the function to remove personalizations options. Now I’d love to get rid of the ‘About yourself’ and Biographical field!

    Excellent post.

  • http://teachingyou.net Morné

    Hi Nancy,

    The way you would get rid of this is with some jquery using the the hide function. So best is to see what the id of that field is.

    Then what you need to do is within the javascript part where the php function name is function hide_personal_options() you will see the last part of the .hide(); Just add another piece $(‘#youridname’).hide(); and that should hide that field with the ID name you specify.

    Hope that helps.

  • HotPot Photos

    Hi Sir, I created a demo user and when I logged in using the demo user and wanted to upload the image it did not give permission to upload image. demo user is a subscriber. do you have any idea.? to solve this

  • http://teachingyou.net Morné

    Hi HotPot,

    This is most probably due to your user not having permissions to edit the profile. Make sure the wordpress user has editing rights.

    You could always remove this code

    if ( !current_user_can( ‘edit_user’, $user_id ) )
    return false;

    You can then just write an if statment to check that if it’s the current user and if the user is actually logged in.

    That should do it for you.

    Thanks

  • http://www.facebook.com/profile.php?id=100002582894145 Muynin Try

    Hello sir , How should i do to hide website box in user profile ?
    I don’t want to show it to user .
    Thank you.

  • Shadez27

    Hi Morné,
    Thanks for the code, works like a charm. :)

    Got any idea on how to check via php if author pic is present, and if not found, display a default image?

    Am planning to remove the whole avatar system and insert this user profiles system on my site.

    Thanks again.

    PS: found the pagination on your tutorials category buggy. wasnt able to goto page 2.

  • http://teachingyou.net Morné

    Hi There,

    You can do a test in a if statement, so what you need to do is to check for empty field or in this situation check if the posted variable is empty.

    So the code will be something like this.

    The code above should help you to get in the right direction.

  • http://teachingyou.net Morné

    Hi There,

    You can just use jQuery and target the id with jQuery(“#theid”).hide();

    This will get you in the right direction.

  • http://twitter.com/mvanboordt Michel van Boordt

    Hi Morné ,
    Is there a way to disable of hide other fields?
    I want to hide the username and screenname fields but can’t find it anywhere

    Thnx

  • http://teachingyou.net Morné

    Hi Michel you are going to have to use jQuery to hide the other fields. Just use the id of the fields you want to hide. jQuery(‘#theid’).hide();

  • http://www.facebook.com/profile.php?id=100004189747043 Mihail Chitu

    Good Morning!
    Please, can you give me the code that I have to add in autor.php page to display the Age and Interest?

  • http://teachingyou.net Morné

    Hi Mihail,

    Why do you want to add it to the author.php file? The code needs to either go in to a plugin or in to your theme’s function.php file.

    Or do you want to display a users form for front end users?

    You can use the code above for this.

    If you want me to create something for you, I’m available for freelance work.

    Thanks

  • Helmi Aditya

    Hi Morne,

    Thanks for the code, it works like a charm!

    Anyway, stupid question, is there any way to display the image in specific size already stated in add_image_size?

    My case is, uploaded images are vary in terms of size, but I really need to display it nicely cropped in 3 different size on 3 different page templates. I was trying to mimic the way post_thumbnails handles the size, but unfortunately it didn’t work.

    Does this possible? If it does, do you have any idea how to do that?

    Thanks again!

  • Hi Morné,

    Hi Morné,

    First of all thanks for the code it help a lot for me. And I just want to ask a related question in this article, How can I hide or unset the Field for First Name and Last Name?

    Thanks Again,

    Warm Regards,

    John

  • http://teachingyou.net Morné

    HI Helmi.

    About the image sizes, I think because this using the author details you won’t be able to add sizes but you would need to manipulate it with css. You could use another script like timthumb to crop it though.

    Regards

  • http://teachingyou.net Morné

    Hi John,

    You could use simple CSS to hide those fields, just target their id or class names.

  • wpsuperplugins

    NICE WORK!

  • http://www.facebook.com/gotzilaza Gotzila Za

    Hi Morné, Thank for your tutorial and i have problem that want to help
    I want to implement your code that upload image have more one field(can add or delete by user)

    below is picture example
    At present i write code textfield part complete and remain upload image part that i don’t how to modify your tutorial code to arrray and save in the wordpress
    Help me to solve this problem?

  • http://teachingyou.net Morné

    Hi Gotzilla,

    I would suggest you try to handle the fields separately for each upload. I’ve never tried handling multiple uploads for the profile itself.

    You will need to modify this part

    update_usermeta( $user_id, ‘image’, $_POST['image'] );

    To be able to handle more images so it will become something like this.

    update_usermeta( $user_id, ‘image_0′, $_POST['image'][0] );
    update_usermeta( $user_id, ‘image_1′, $_POST['image'][1] );

    update_usermeta( $user_id, ‘image_2′, $_POST['image'][2] );
    Please note this is untested code but should give you an idea of what needs to happen.

    Hope that helps ;)