User avatar upload frontend

Put this code functions.php file then you can change user profile image.

function custom_avatar_field( $user ) { ?>
<?php  wp_enqueue_media(); ?>

jQuery(document).ready(function($){
var custom_uploader;
$(‘#upload_image_button’).click(function(e) {

e.preventDefault();

//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}

//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: ‘Choose Image’,
button: {
text: ‘Choose Image’
},
multiple: false
});

//When a file is selected, grab the URL and set it as the text field’s value
custom_uploader.on(‘select’, function() {
attachment = custom_uploader.state().get(‘selection’).first().toJSON();
$(‘#upload_image’).val(attachment.id);
$(‘#image’).attr(‘src’, attachment.url);
});

//Open the uploader dialog
custom_uploader.open();

});

});

<h3>Ozone Avatar</h3>
<table>
<tr>
<td><label for=”be_custom_avatar”>Avatar Image:</label></td>
<td><label for=”upload_image”>
<input id=”upload_image” type=”hidden” size=”36″ name=”ad_image” value=”<?php echo get_user_meta($user->ID,’be_custom_avatar’,true)?>” />
<input id=”upload_image_button” type=”button” value=”Upload Image” />
</label></td>
<td><img src=”<?php echo wp_get_attachment_url(get_user_meta($user->ID,’be_custom_avatar’,true));?>” id=”image”  style=”height:100px;”/></td>
</tr>
</table>
<?php
}
add_action( ‘show_user_profile’, ‘custom_avatar_field’ );
add_action( ‘edit_user_profile’, ‘custom_avatar_field’ );

/**
* Save Ozone Avatar Field
*@author Ommune Team
* @link http://www.ommune.com/
* @param int $user_id
*/
function save_custom_avatar_field( $user_id ) {
if ( !current_user_can( ‘edit_user’, $user_id ) ) { return false; }
update_user_meta( $user_id, ‘be_custom_avatar’, $_POST[‘ad_image’] );
}
add_action( ‘personal_options_update’, ‘save_custom_avatar_field’ );
add_action( ‘edit_user_profile_update’, ‘save_custom_avatar_field’ );

/**
* Use Ozone Avatar if Provided
* @author Ommune Team
* @link http://www.ommune.com/
*
*/
function wp_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) {
$custom_avatar =  get_the_author_meta(‘be_custom_avatar’,$id_or_email);
if ($custom_avatar)
$return = get_wp_user_avatar_image($id_or_email, $size, $default, $alt);
elseif ($avatar)
$return = $avatar;
else
$return = ‘<img src=”‘.$default.'” width=”‘.$size.'” height=”‘.$size.'” alt=”‘.$alt.'” />’;
return $return;
}
add_filter(‘get_avatar’, ‘wp_gravatar_filter’, 10, 5);

// Find avatar, show get_avatar if empty
function get_wp_user_avatar_image($id_or_email=””, $size=’96’, $align=””, $alt=””, $email=’unknown@gravatar.com’){
global $avatar_default, $blog_id, $post, $wpdb, $_wp_additional_image_sizes;
// Checks if comment
if(is_object($id_or_email)){
// Checks if comment author is registered user by user ID
if($id_or_email->user_id != 0){
$email = $id_or_email->user_id;
// Checks that comment author isn’t anonymous
} elseif(!empty($id_or_email->comment_author_email)){
// Checks if comment author is registered user by e-mail address
$user = get_user_by(’email’, $id_or_email->comment_author_email);
// Get registered user info from profile, otherwise e-mail address should be value
$email = !empty($user) ? $user->ID : $id_or_email->comment_author_email;
}
$alt = $id_or_email->comment_author;
} else {
if(!empty($id_or_email)){
// Find user by ID or e-mail address
$user = is_numeric($id_or_email) ? get_user_by(‘id’, $id_or_email) : get_user_by(’email’, $id_or_email);
} else {
// Find author’s name if id_or_email is empty
$author_name = get_query_var(‘author_name’);
if(is_author()){
// On author page, get user by page slug
$user = get_user_by(‘slug’, $author_name);
} else {
// On post, get user by author meta
$user_id = get_the_author_meta(‘ID’);
$user = get_user_by(‘id’, $user_id);
}
}
// Set user’s ID and name
if(!empty($user)){
$email = $user->ID;
$alt = $user->display_name;
}
}
// Checks if user has avatar
$wpua_meta = get_the_author_meta($wpdb->get_blog_prefix($blog_id).’user_avatar’, $email);
$wpua_meta = get_the_author_meta(‘be_custom_avatar’,$email);

// Add alignment class
$alignclass = !empty($align) && ($align == ‘left’ || $align == ‘right’ || $align == ‘center’) ? ‘ align’.$align : ‘ alignnone’;
// User has avatar, bypass get_avatar
if(!empty($wpua_meta)){
// Numeric size use size array
$get_size = is_numeric($size) ? array($size,$size) : $size;
// Get image src
$wpua_image = wp_get_attachment_image_src($wpua_meta, $get_size);
$dimensions = is_numeric($size) ? ‘ width=”‘.$wpua_image[1].'” height=”‘.$wpua_image[2].'”‘ : “”;
// Construct the img tag

$avatar = ‘<img src=”‘.$wpua_image[0].'”‘.$dimensions.’ alt=”‘.$alt.'” />’;
} else {
// Get numeric sizes for non-numeric sizes based on media options
if(!function_exists(‘get_intermediate_image_sizes’)){
require_once(ABSPATH.’wp-admin/includes/media.php’);
}
// Check for custom image sizes
$all_sizes = array_merge(get_intermediate_image_sizes(), array(‘original’));
if(in_array($size, $all_sizes)){
if(in_array($size, array(‘original’, ‘large’, ‘medium’, ‘thumbnail’))){
$get_size = ($size == ‘original’) ? get_option(‘large_size_w’) : get_option($size.’_size_w’);
} else {
$get_size = $_wp_additional_image_sizes[$size][‘width’];
}
} else {
// Numeric sizes leave as-is
$get_size = $size;
}
// User with no avatar uses get_avatar
$avatar = get_avatar($email, $get_size, $default=””, $alt=””);
// Remove width and height for non-numeric sizes
if(in_array($size, array(‘original’, ‘large’, ‘medium’, ‘thumbnail’))){
$avatar = preg_replace(‘/(width|height)=”d*”s/’, “”, $avatar);
$avatar = preg_replace(“/(width|height)=’d*’s/”, “”, $avatar);
}
$str_replacemes = array(‘wp-user-avatar ‘, ‘wp-user-avatar-‘.$get_size.’ ‘, ‘wp-user-avatar-‘.$size.’ ‘, ‘avatar-‘.$get_size, ‘photo’);
$str_replacements = array(“”, “”, “”, ‘avatar-‘.$size, ‘wp-user-avatar wp-user-avatar-‘.$size.$alignclass.’ photo’);
$avatar = str_replace($str_replacemes, $str_replacements, $avatar);
}
return $avatar;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s