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:


<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:


$userLogin= $wpdb->get_var("SELECT $wpdb->users.user_login FROM $wpdb->users WHERE $wpdb->_users.ID = ".$someUserID.");

if(file_exists(ABSPATH."wp-content/images/".$userLogin.".jpg"))
	$imageURL = get_bloginfo('url')."/wp-content/images/".$userLogin.".jpg";
else
	$imageURL = get_bloginfo('url')."/wp-content/images/unknown.jpg";;

echo "<img src=\"$imageURL\" />";

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:

65 responses to “Add User Photo”

Click to show trackbacks

15 Jan 2007
tricky Identicon Icon tricky (04:25:16) :

does this image get resized or thumbnailed?

15 Jan 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (07:09:36) :

Currently: no.

16 Jan 2007
Cruella DeVille Identicon Icon Cruella DeVille (11:15:43) :

Hi.
I’ve managed to activate the plugin, upload photos, but how do I display them?

If you could help me out here, I’d be very greatful!

16 Jan 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (13:05:41) :

Cruella,

I’ve added an example above. That should give you a good start.

16 Jan 2007
Cruella DeVille Identicon Icon Cruella DeVille (13:13:24) :

Great!
Thankyou

21 Jan 2007
Rubira Identicon Icon Rubira (14:39:00) :

Hi

Can you give us a better explanation about this plugin?

Thanxs :)

21 Jan 2007
Rubira Identicon Icon Rubira (14:39:44) :

Your example doesn’t work.

21 Jan 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (15:06:40) :

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.

19 Feb 2007
tricky Identicon Icon tricky (09:32:41) :

You could use a cropping feature (optional) like the one at:

http://theblemish.com/post-thumbs-plugin/

in order to use it as an avatar…

21 Feb 2007
HH Identicon Icon HH (19:35:59) :

Rubira did you enclose the PHP code in “” (without the space between

21 Feb 2007
HH Identicon Icon HH (20:02:39) :

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.

22 Feb 2007
HH Identicon Icon HH (10:28:58) :

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!

22 Feb 2007
Markku M Identicon Icon Markku M (10:55:43) :

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.

22 Feb 2007
HH Identicon Icon HH (18:55:41) :

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

22 Feb 2007
HH Identicon Icon HH (19:47:45) :

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)

23 Feb 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (12:52:27) :

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.

23 Feb 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (12:54:01) :

Another request: could you please add a check that if the upload field is empty

Yes, I’ll be including this in the next release.

23 Feb 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (12:58:11) :

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:
[php]
($_FILES['photoUpload']['type'] == “image/pjpeg”)
[/php]

This makes line 61:
[php]if(($_FILES['photoUpload']['type'] == “image/jpeg”) || ($_FILES['photoUpload']['type'] == “image/jpg”) || ($_FILES['photoUpload']['type'] == “image/pjpeg”)){
[/php]

Hope that helps for now.

06 Mar 2007
Rusty Miller Identicon Icon Rusty Miller (15:15:36) :

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.

06 Mar 2007
Cruella DeVille Identicon Icon Cruella DeVille (17:31:20) :

Rusty Miller: If you take a look at this page: http://studblogg.uib.no/?author=5 you can see what the plugin “does”.

06 Mar 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (17:45:08) :

Rusty, please see the usage information above.

07 Mar 2007
Rusty Miller Identicon Icon Rusty Miller (09:06:19) :

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?

07 Mar 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (09:20:02) :

The only thing that comes to mind are Gravatars, http://codex.wordpress.org/Using_Gravatars

08 Mar 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (15:07:36) :

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.

17 Mar 2007
sofia Identicon Icon sofia (20:39:39) :

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

17 Mar 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (21:43:34) :

Sofia,

It’s currently not possible to upload images on behalf of other people. That seems like a good feature, so I’ll add that to the list for inclusion in the next release.

16 May 2007
Yotsume Identicon Icon Yotsume (17:32:06) :

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

17 May 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (08:09:43) :

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.

28 May 2007
Thomas Identicon Icon Thomas (07:49:00) :

Yo lo puse así y funca bien:

‘; ?>

01 Jun 2007
billy Identicon Icon billy (14:11:09) :

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.

04 Jun 2007
Andrew Ferguson Identicon Icon Andrew Ferguson (02:48:14) :

@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:

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

Then use the $username variable to access their picture:

[html]
08 Jun 2007

S. Identicon Icon S. (05:25:52) :

Hey Andrey,
thanks for the plug in - it’s rather… badly written. Otherwise after I rewrote it it’s fine.

S.

18 Jun 2007
Marci Identicon Icon Marci (18:08:26) :

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?

19 Jun 2007
Marci Identicon Icon Marci (00:41:57) :

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. :)

19 Jun 2007
HH Identicon Icon HH (17:19:59) :

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

old line 62:
[php] move_uploaded_file($_FILES['photoUpload']['tmp_name'], ABSPATH.”/wp-content/images/”.$userdata->user_login.”.jpg”); [/php]

replace with 3 lines:
[php]
$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
[/php]

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

19 Jun 2007
Marci Identicon Icon Marci (18:06:56) :

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 :)

26 Aug 2007
The Big Chorizo Identicon Icon The Big Chorizo (15:41:45) :

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!

03 Sep 2007
Martin Identicon Icon Martin (23:07:01) :

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

03 Sep 2007
Martin Identicon Icon Martin (23:15:44) :

Oops.. One more thing Sofia..
You also have to add the:

enctype=”multipart/form-data”>

to the

03 Sep 2007
Martin Identicon Icon Martin (23:18:09) :

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.

03 Sep 2007
Martin Identicon Icon Martin (23:30:23) :

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.

08 Sep 2007
chag Identicon Icon chag (06:52:53) :

Hi

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

add this 2 functions

[php]
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);
}

[/php]

juste before this line :

[php]
add_action(’show_user_profile’, ‘fergcorp_addToUser_addFields’);
[/php]

And add this 2 lines :
[php]
list($width, $height) = getimagesize($final_path);
redimage($final_path, $width,$height);
[/php]

after the move_uploaded_file command :

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

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

[php]
$newsize = calcratio($src_w, $src_h, 120, 150);
[/php]

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

19 Oct 2007
roj Identicon Icon roj (02:41:24) :

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.

19 Oct 2007
chag Identicon Icon chag (07:56:33) :

Hi,

what error message ?

19 Oct 2007
roj Identicon Icon roj (10:10:36) :

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.

20 Dec 2007
tukangKOMENTAR Identicon Icon tukangKOMENTAR (19:28:27) :

I still get error msg: Error: Please only upload jpg files! Your content type is. (without pjpeg), every time I hit Update Profile button. Any idea why?

06 Mar 2008
Mike Smith Identicon Icon Mike Smith (21:32:48) :

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

06 Mar 2008
Mike Smith Identicon Icon Mike Smith (21:42:26) :

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="" />
13 Aug 2008
Aike Identicon Icon Aike (09:29:54) :

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

16 Aug 2008
Andrew Ferguson Identicon Icon Andrew Ferguson (20:18:44) :

@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){

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Code: To include code, bracket it with [code language="php"] and [/code].
Example: [code language="php"] echo "Hello, World!"; [/code]

Plugin Help: If your requesting help about a plugin, please provide as much information as possible. At the very least, include your WordPress Version and the Plugin Version!