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






does this image get resized or thumbnailed?
Currently: no.
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!
Cruella,
I’ve added an example above. That should give you a good start.
Great!
Thankyou
Hi
Can you give us a better explanation about this plugin?
Thanxs
Your example doesn’t work.
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.
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…
Rubira did you enclose the PHP code in “” (without the space between
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.
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!
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.
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
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)
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.
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.
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.
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.
Rusty Miller: If you take a look at this page: http://studblogg.uib.no/?author=5 you can see what the plugin “does”.
Rusty, please see the usage information above.
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?
The only thing that comes to mind are Gravatars, http://codex.wordpress.org/Using_Gravatars
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.
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
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.
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
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.
Yo lo puse así y funca bien:
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.
@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
$usernamevariable to access their picture:[html]-
S. (05:25:52) :
-
18
Jun
2007
-
Marci (18:08:26) :
-
19
Jun
2007
-
Marci (00:41:57) :
-
19
Jun
2007
-
HH (17:19:59) :
-
19
Jun
2007
-
Marci (18:06:56) :
-
26
Aug
2007
-
The Big Chorizo (15:41:45) :
-
03
Sep
2007
-
Martin (23:07:01) :
-
03
Sep
2007
-
Martin (23:15:44) :
-
03
Sep
2007
-
Martin (23:18:09) :
-
03
Sep
2007
-
Martin (23:30:23) :
-
08
Sep
2007
-
chag (06:52:53) :
-
19
Oct
2007
-
roj (02:41:24) :
-
19
Oct
2007
-
chag (07:56:33) :
-
19
Oct
2007
-
roj (10:10:36) :
-
20
Dec
2007
-
tukangKOMENTAR (19:28:27) :
-
06
Mar
2008
-
Mike Smith (21:32:48) :
-
06
Mar
2008
-
Mike Smith (21:42:26) :
-
13
Aug
2008
-
Aike (09:29:54) :
-
16
Aug
2008
-
Andrew Ferguson (20:18:44) :
08 Jun 2007
Hey Andrey,
thanks for the plug in - it’s rather… badly written. Otherwise after I rewrote it it’s fine.
S.
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?
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.
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.
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
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!
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
Oops.. One more thing Sofia..
You also have to add the:
enctype=”multipart/form-data”>
to the
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.
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.
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
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.
Hi,
what error message ?
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.
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?
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.
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
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
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
@Aike:
If the MIME type is not
image/jpgorimage/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
About
Email:
Hometown: Seattle, Washington
Location: Golden, Colorado
School: Colorado School of Mines Mobile: (206) 569-4410
This is the story of me:
My hopes, my dreams, my aspirations.
My trials and tribulations.
My life.
Recent Comments
Facebook Status
Donate
If you enjoy AFdN or find my plugins very useful; consider making a dontation, please?
Countdown Timer
in 8 months, 18 days, 1 hour, 47 minutes, 13 seconds
Photos
Andrew Ferguson's Photos
Portfolio
Pages
Categories
Archives
Meta
Subscribe
Something or Other
Support Bloggers' Rights!
Stats