Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Created May 1, 2012 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmynotjim/2570530 to your computer and use it in GitHub Desktop.
Save jimmynotjim/2570530 to your computer and use it in GitHub Desktop.
Add user avatar uploader to admin panel and function call for front end
<?php
/* BEGIN: add user info
---------------------------------------------------------------------------------------------------- */
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
function 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">Profile image must be at least 127x127 to display properly at all screen sizes</span>
</td>
</tr>
</table>
<?php }
/* Add the image loader */
function 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','profile_upload_js');
function my_admin_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
function my_admin_styles() {
wp_enqueue_style('thickbox'); //thickbox styles css
}
/* Save the data */
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'image', $_POST['image'] );
}
/* BEGIN: User Avatar
Avatars should be at least 127x127
When calling make sure to include a user ID or variable to pull it from the loop
---------------------------------------------------------------------------------------------------- */
function user_avatar($ID) {
$img_url = get_the_author_meta( 'image', $ID);
if ( '' != $img_url ) {
echo '<img src="'.$img_url.'" />';
}
else { echo '<img src="'.get_bloginfo( "template_url" ).'/assets/img/authors/missing.jpg" />';
}
}
/* End User Avatar */
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment