Add User Photo

Overview
Allows adding a photo to a user profile

Compatibility

  • 2.1.*
  • 2.0.*
  • 1.5 (in theory, not tested)

Installation
Download the file and put it in your plugins directory. In your wp-content folder, create a directory called images and make sure that it was write permissions.

Modify line 21 of /wp-admin/profile.php, adding enctype="multipart/form-data" to the form tag. So at the end of the day, line 21 should look like:

</p>
<form name="profile" id="your-profile" action="profile-update.php" method="post" enctype="multipart/form-data">

Activate plugin.

Usage
User will now be able to upload a jpg-based image. The image is uploaded and renamed to their username with a jpg extension (e.g. johndoe.jpg).

The file is located in /wp-content/images/

To use the file, simply reference its location. This can be done using an absolute reference (i.e. pointing to an image at http://www.example.com/wp-content/images/johndoe.jpg) or through PHP programming.

Example:

If you know the UserID, but not the username, you can do a simple lookup:

<br />
$userLogin= $wpdb->get_var("SELECT $wpdb->users.user_login FROM $wpdb->users WHERE $wpdb->_users.ID = ".$someUserID.");</p>
<p>if(file_exists(ABSPATH."wp-content/images/".$userLogin.".jpg"))<br />
	$imageURL = get_bloginfo('url')."/wp-content/images/".$userLogin.".jpg";<br />
else<br />
	$imageURL = get_bloginfo('url')."/wp-content/images/unknown.jpg";;</p>
<p>echo "<img src=\"$imageURL\" />";<br />

In the above example, an addition file, unknown.jpg, has been placed in the images directory to serve as a placeholder for users who haven’t uploaded images. You also need to specify the value of $someUserID.

Download
Latest Version:

95 Replies to “Add User Photo”

  1. Can you give us a better explanation about this plugin?

    Not really. It’s a relatively straight forward plugin that allows you to upload a user photo to a directory. Can you be more specific?

    Your example doesn’t work.

    That should work just fine. I pretty much just copied and pasted it from the production version. I went back and clarified some more information about the user ID.

    Also, just saying, “Your example doesn’t work,” isn’t very helpful. There are a million different issues that might cause a piece of PHP code to choke. I’m not a mind reader. Details about how your implementing the code and any error messages you’re receiving are good things to include as well.

  2. Found another possible reason… check the file permission of the image uploaded. If it’s not readable by “Public” then that could be the cause. Andrew, is there a way to force the permission setting?

    Please remove my last comment since the less-than sign got stripped out.

  3. Another request: could you please add a check that if the upload field is empty, the code should NOT check the file type for jpg and allow the rest of the update to be successful? This is a scenario where a photo is already uploaded, but the user wants to change something else in the profile. The user should not be forced to upload an image every time.

    The current work around is to just ignore the error. The rest of the information seem to update OK.

    Thanks!

  4. For some reason even if taking format.jpg into the system it gives an error message:

    Error: Please only upload jpg files! Your content type is image/pjpeg

    What went wrong? Think this plugin can not handle jpeg- format files properly?

    Still would like to use your plugin! Any advise appreciated.

  5. Pingback: Planet Wordpress
  6. Markku,

    Mine works, but from your output, it seems your file name had an extension of “pjpeg”.

    ——————————- also ———————–
    I have the fix for the file permission on non-Windows systems. Replace line 76 in the php file with:

    $final_path = ABSPATH.”/wp-content/images/”.$userdata->user_login.”.jpg”;
    move_uploaded_file($_FILES[‘photoUpload’][‘tmp_name’], $final_path);
    chmod($final_path, 0644); // rw for owner, r for everyone else

  7. Here is the last fix from me to not output error when there the user isn’t uploading a file, but just changing the rest of the profile:

    replace the line with “else” with

    elseif (strlen($_FILES[‘photoUpload’][‘name’])>0)

  8. Andrew, is there a way to force the permission setting?
    I don’t think so. I might be able to include a shell script, but you have to make it executable, which sort of defeats the purpose of automating the entire thing (i.e. you either change the folder permissions or you change a file to permission so that you can automatically change the folder permissions).

    I could be, I’ll take a look.

  9. For some reason even if taking format.jpg into the system it gives an error message:
    Error: Please only upload jpg files! Your content type is image/pjpeg

    This is a known error that I actually forgot about before I released it. It will be fixed one way or another in the next release.

    As a temporary fix, you can modify line 61 by adding:

    ($_FILES['photoUpload']['type'] == "image/pjpeg")
    

    This makes line 61:

    if(($_FILES['photoUpload']['type'] == "image/jpeg") || ($_FILES['photoUpload']['type'] == "image/jpg") || ($_FILES['photoUpload']['type'] == "image/pjpeg")){
    

    Hope that helps for now.

  10. 1. I’ve activated your Plugin and it’s working, but I’m not sure what the intended use is.

    For example, I thought your Plugin might display an uploaded user’s photo next to their comments, but that’s not happening.

    What exactly is the purpose of a user uploading their photo if no-one else can see it?

    2. Please give more details about your example PHP code, in layman’s terms. What does that code “do” exactly? Where might someone paste that code?

    Thanks in advance.

  11. Now I understand the purpose of your Plugin. Thank you for clarifying.

    As you can tell, I’m more interested in an “avatar” Plugin, to show a picture next to a user comment.

    Any recommendations?

  12. General Note:

    I just added a donation button on the side bar (to the right), so if you *really* like my plugins (and/or me) you might consider making a donation. I’ve been spending more and more time writing and supporting plugins. I?m a college student (3rd year Elect. Eng.) and really only do this programming thing on the side for the love of it.

  13. hi andrew,
    I was wondering if it was possible to use this plugin to upload images on the authors of a blog when you edit their profiles as the administrator.

    At the moment it only works when I edit my own administrator profile, but I want to be able to use it also with the profile of the other users… is there an easy way to do this?

    thanks

  14. Ok, Your plugin here is a super bug in code in to many places! .jpg .JPG neither work! No way my photos are not jpg format and your plug in still kicks back only an error of: Error: Please only upload jpg files! Your content type is

    Ok time to hunt for a better coded profile plug-in

  15. Yotsume,

    It’s a pretty alpha stage code. It’s mostly designed as a starting place for other coders to use or for PHP guru’s to play with. Sorry you’re having such a hard time.

    As for your error, the plugin is checking the MIME type. If the file you upload has not been correctly identified as an “image/jpeg”, then it won’t work.

  16. okay i have been trying, for months now, to display a nice user profile page with the userpic in it. i tried dans avatar thing (the modded version with userpics) and it only bring sup your usericon or no icon if you are not logged in. your plugin looked promising howver, i need to to automatically bring up a certain users pic rather than putting in the id. That way when you click on a users nickname from an authors (users) generated link, it goes to their generated profile and brings up the pic… if that makes sense. i need /?author=1 to come up with that author’s (1) pic.

  17. @billy:

    Shouldn’t be too hard. You’ll need to do a bit of coding.

    The first step will be to get the username from the userid. This is easily accomplished using mySQL query to the DB. Based on your above variable scheme:

    $username = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE ID = ".$_REQUEST['author']);
    

    Then use the $username variable to access their picture:

    <a href="http://www.example.com/location/to/images/<?php echo $username; ?>.jpg" alt="Image of <?php echo $username; ?>" />
    
  18. Hey Andrey,
    thanks for the plug in – it’s rather… badly written. Otherwise after I rewrote it it’s fine.

    S.

  19. Hey Andrew, this is really cool, and I want it to work!!

    Right now, when I click the update button with a jpg file ready to upload, I get several php errors, which appear to be permission related. I have wp-content/images/ with pemissions set to 644 (also tried 766)

    Warning: move_uploaded_file(/home/christia/public_html//wp-content/images/admin.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/christia/public_html/wp-content/plugins/addUserPhoto.php on line 62

    Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘/tmp/phpqK8rkq’ to ‘/home/christia/public_html//wp-content/images/admin.jpg’ in /home/christia/public_html/wp-content/plugins/addUserPhoto.php on line 62

    Warning: Cannot modify header information – headers already sent by (output started at /home/christia/public_html/wp-content/plugins/addUserPhoto.php:62) in /home/christia/public_html/wp-includes/pluggable.php on line 275

    I considered the solution offered by HH (it sounded like it might be related) but he says to replace line 76 in the php file — and there is no line 76 in adduserphoto.php.

    Any thoughts on what else I can try?

  20. I discovered that this error was happening in the regular write post upload area as well, so I went hunting on the WP support forums, and found several relevant threads. Especially relevant were:

    http://wordpress.org/support/topic/107994
    http://wordpress.org/support/topic/102139

    In both threads the conclusion was that this was something only the host could fix (unless you have root access). So I contacted my host, and the support tech created the wp-content/images/ folder for me, and gave it write permissions. His magic touch or command authority solved the problem, and I can upload both in write pages, and in AddUserPhoto. Hurray! Hope this helps others. 🙂

  21. Marci,
    You are correct, there is no line 76… My mistake. It was supposed to be line 62. So:

    old line 62:

       move_uploaded_file($_FILES['photoUpload']['tmp_name'], ABSPATH."/wp-content/images/".$userdata->user_login.".jpg"); 

    replace with 3 lines:

       $final_path = ABSPATH."/wp-content/images/".$userdata->user_login.".jpg";
       move_uploaded_file($_FILES['photoUpload']['tmp_name'], $final_path);
       chmod($final_path, 0644); // rw for owner, r for everyone else
    

    Sorry about the previous error. Hope this works for you.

  22. Thanks HH. 🙂 I did get it working (my comment explaining what I did — or rather, what my host did — probably was still in moderation when you wrote). But next time I put this on a site (and I will!) I’ll definitely give this a try. Thanks for the clarification! And thanks again to Andrew for a great plugin. This is so needed, considering what I’ve seen on the forum.
    Marci 🙂

  23. Thanks for the plugin but has anybody solved the bug that Yotsume mentioned: “Error: Please only upload jpg files! Your content type is”

    I’m having the same problem using WP2.2. I even edited the photo in fireworks to make sure it was jpg and it still returns this error.

    This plugin is ideal for what I’m trying to do, just a photo for each user that can be displayed on his profile or author page. No strange gravatar things that normal people don’t understand 🙂

    Keep up the good work, Andrew!

  24. This is for Sofia…
    I found I can upload photos for my members by duplicating the line:

    add_action(‘show_user_profile’, ‘fergcorp_addUserFolder_addField’);

    and changing the duplicated line to ‘edit_user_profile’

    so you will have three lines then:

    add_action(‘show_user_profile’, ‘fergcorp_addUserFolder_addField’);
    add_action(‘edit_user_profile’, ‘fergcorp_addUserFolder_addField’);
    add_action ‘personal_options_update’, ‘fergcorp_addUserPhoto_updateField’);

    Hope this helps.
    Martin

  25. Sorry, Got cut off.
    Oops.. One more thing Sofia..
    You also have to add the:

    enctype=”multipart/form-data” (see top of this page)

    to line 76 of user_edit.php file in the admin folder if using WordPress 2.1.2.
    It may be some other line in a diff version.

  26. This is for the The Big Chorizo:
    Normally the error message you get would be “Error: Please only upload jpg files! Your content type is image/pjpeg”

    For a while I was getting the msg w/o the type printing out and found that I had not added the enctype=”multipart/form-data” (with a trailing greater than) to the profile.php file. Not sure if the orig. poster just forgot to type the rest ofthe error msg, but hope this helps.

    p.s. had to put trailing greater than as using the symbol may cut off the comment.

  27. Hi

    I wrote a small code to automatically resize the uploaded pictures.

    add this 2 functions

           function calcratio($src_w, $src_h, $dst_w, $dst_h){
                    $ratio_orig = $src_w/$src_h;
                    if ($dst_w/$dst_h > $ratio_orig) {
                       $dst_w = $dst_h*$ratio_orig;
                    } else {
                       $dst_h = $dst_w/$ratio_orig;
                    }
    
                    $result=array($dst_w, $dst_h);
                    return $result;
            }
    
            function redimage($filename,$src_w,$src_h) {
                    $newsize = calcratio($src_w, $src_h, 120, 150);
                    $dst_im = ImageCreateTrueColor($newsize[0],$newsize[1]);
                    $src_im = imagecreatefromjpeg($filename);
    
                    ImageCopyResampled($dst_im,$src_im,0,0,0,0,$newsize[0],$newsize[1],$src_w,$src_h);
                    ImageJpeg($dst_im,$filename);
    
                    ImageDestroy($dst_im);
                    ImageDestroy($src_im);
            }
    
    

    juste before this line :

    add_action('show_user_profile', 'fergcorp_addToUser_addFields');
    

    And add this 2 lines :

          list($width, $height) = getimagesize($final_path);
          redimage($final_path, $width,$height);
    

    after the move_uploaded_file command :

          move_uploaded_file($_FILES['photoUpload']['tmp_name'], $final_path);
          chmod($final_path, 0644); // rw for owner, r for everyone else
    

    This code require the GD2 library. The resized size can be set on this line :

    $newsize = calcratio($src_w, $src_h, 120, 150);
    

    Actually, the image size is 120px width and 150px height

    The calcratio function will keep the image aspect when the image is resized. Hope it helps.

    You can see the complete source code here : http://pastebin.com/f2002953b

    Chag

  28. I think this is better than using gravatar.. Thank you for this plugin.

    to chad: tried your code but it gives me the error message even if I have no user photo.

  29. to chag: this is the error

    Error:Please only upload jpg files! Your content type is
    Please go back and try again.

    …but I managed to avoid the error by adding

    || ($_FILES[‘photoUpload’][‘type’] == “” at the IF statement of

    fergcorp_addToUser_updateFields()

    I’m not sure if that’s the right solution but after adding it, now it’s working.

  30. Hey Andrew,

    This is a great plugin. I tried using your db code to have the image show the current author’s image if you’re viewing the author but I get an error.

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
    SELECT user_login FROM wp_users WHERE ID =

    This is the code I put at the top of the author page:

    <?php $username = $wpdb->get_var("SELECT user_login FROM $wpdb->users WHERE ID = ".$_REQUEST['author']); ?>

    I’m not sure what I could be doing wrong? I’d love to get this working because I’ve got a project that will have 6-7 authors so I’d like their images showing automatically.

    Thanks

  31. Just so everyone knows, as SOON as I posted that comment, I figured this out 🙂

    WordPress has you add this to your author page to get the current user.

    <?php
    if(get_query_var('author_name')) :
    $curauth = get_userdatabylogin(get_query_var('author_name'));
    else :
    $curauth = get_userdata(get_query_var('author'));
    endif;
    ?>

    So this way, you can use codes to show the name, login name, description, ect. All I did was replace the get $username from my above comment, and replaced it with this code

    <img src="http://www.yourwebsite.com/wp-content/images/<?php echo $curauth->user_login; ?>.jpg" alt="" />
  32. Auf Deutsch kann mit keiner bei dem Plugin helfen oder? Ich bekomme keine Fotos hochgeladen steht immer:

    Error: Please only upload jpg files! Your content type is

    Please go back and try again.

    ich habe es mit allen jpg Datein versucht, groß klein aber nicht

  33. @Aike:

    If the MIME type is not image/jpg or image/jpeg, the file will not upload. It is possible to have the plugin not check the file type, however users would be able to upload any type of file at the point, which would be security risk.

    If you are okay with that, then change line 61 from:
    if(($_FILES['photoUpload']['type'] == "image/jpeg") || ($_FILES['photoUpload']['type'] == "image/jpg")){
    to
    if(1){

  34. Hi Andrew,

    After uploaded my image, I receive the following error message: “Upload error: Unable to place the user photo at: /home/restricted/home/MYSITE/public_html/wp-content/uploads/userphoto/admin.JPG.
    I checked the permissions and the uploads folder settings and it seems to be ok.
    Regards.

  35. hi
    I downloaded the file and made it in wp-content/plugins folder and i changed the form to encrypt=multipart/ settings in wpadmin/profile.php
    after this i go and check the profile page i didnt find the field and input box in that page.pls tell me how to do it. i need to add create table for that image or how to make it work for me

  36. How has all of this changed with WordPress 2.7? I see one item is different – the /wp-admin/profile.php file is actually /wp-admin/user-edit.php

    Also, trying to put the author’s photo in their post – keep in mind we have multiple authors for this blog. Any advice or code to help me modify the index.php file to do this?

    1. “I see one item is different – the /wp-admin/profile.php file is actually /wp-admin/user-edit.php”

      That’s inaccurate. profile.php is a central part of the whole WP process.

  37. I posted a detailed request for help, and it appears to have disappeared. Before I retype the whole thing, I will look and see what happens to this.

  38. Ok, let me try this again.

    I can’t edit line 21 of /wp-admin/profile.php because the file is only 20 lines long.

    The complete file is below. Please tell me how I modify it to get this plugin to work.

  39. Does this have the ability to upload more than one picture per profile? I am dying for a solution to make this happen.

  40. Andrew,
    I just installed this – and it appears to be be working although I did not edit the profile.php (when I did it caused syntax errors).

    Is there a way to get the photos to show up in a sidebar?

    Thanks for this plugin!

    Tina

  41. It’s a great idea, I’ve just never really understood why this isn’t part of the core as an option.
    Concerning your plugin, have you investigated is there a possibility to add option to an user profile without modifying the core? Send the plugin to the developement team, you never know, it might get accepted.

    1. @Toni L

      I haven’t done any work on this plugin since WordPress 2.1 was released. At the time, there was no way to interface without hacking (i.e. mod-ing) the core. However, I would imagine that there are some new hooks that could be used. Unfortunately, I don’t have the time to continue to develop this on my own time, unless someone where to sponsor development.

  42. Am pus codul in profile.php, si la profil a aparut adauga foto, dar nu pot incarca foto, primesc mesajul de eroare(Error: Please only upload jpg files!) cu toate ca imaginea mea e .jpg

    Eu nu lipi?i codul din exemplul dvs. (poate asta e problema), deoarece nu am ?tiut unde s?-l pun?
    Te rog ajuta ma sa inteleg unde sa il pun

  43. I put the code in profile.php, and appeared to profile add photos, but I can not upload photos, get the error message (Error: Please only upload jpg files!) although my image is. jpg

    I do not paste the code from your example (maybe that’s the problem) because I did not know where to put it?
    Please help me understand where to put it

  44. Hello Andrew,

    I was wondering how can I add the photo to the author.php page, I was using user photo, which had a couple of commands that I could add to call the photo.
    * userphoto_the_author_photo()
    * userphoto_the_author_thumbnail()
    * userphoto_comment_author_photo()
    * userphoto_comment_author_thumbnail()

    Do you have similar commands for your plugin?

    Also, I have over 200 people in my company. I was wondering if I have your plugin installed, and I place all 200 photos in the images folder will it let them be associated to that person. or will I need to go into each persons profile and upload the photo for them.

    Thanks,

    Aaron

    1. @Aaron:

      I haven’t done much with this code recently. The code really just serves to provide a couple of hooks to make it easier to upload the data and leaves it up to you to develop the code to bring back out.

      Take a look at previous comments; if you still need help developing the code, I am available for hire at very reasonable rates.

  45. Hello Andrew,

    I have tried implementing as you suggested; however, I do not have a line 21. and when I try changing line 61 to if(1){ and then I try uploading a photo it goes through the motions, but no image is uploaded.

    Before, it gave me the same error that Aike had, Error: Please only upload jpg files! Your content type is.

    Do you know how I can get it to upload?

    Thanks,

    Aaron

    1. @Aaron:

      As listed above, please note the compatibility of this plugin is only for:

      • 2.1.*
      • 2.0.*
      • 1.5 (in theory, not tested)

      If you need help updating the code for a current version of WordPress, I am available for hire at very reasonable rates.

  46. Hi Andrew,

    Could you update your plugin and add a simple JQuery to replace the form encrypt
    $('form#your-profile').attr((this.encoding ? 'encoding' : 'enctype') , 'multipart/form-data');

    and that way users wont have to edit core files.

    Bainternet.

  47. @bainternet:

    I haven’t worked on this code for sometime. I should probably update this page to mention that I’ve unfortunately had to abandon this code. Part of the reason is that this code came from project that I now longer work on, the other part is that I just don’t have enough time to maintain this code for free.

    If you need help updating the code for a current version of WordPress (which could include implementing jQuery), I am available for hire at very reasonable rates.

    I would also encourage you to fork the code and create your own version.

Leave a Reply

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