Skip to content

Instantly share code, notes, and snippets.

@tojibon
Created June 11, 2015 10:06
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 tojibon/7ea9dd2fe2f20d88eaa1 to your computer and use it in GitHub Desktop.
Save tojibon/7ea9dd2fe2f20d88eaa1 to your computer and use it in GitHub Desktop.
Saving/Editing user profile photo on WordPress admin
.wp_slrp_img_wrap {
max-width: 160px;
text-align: right;
margin-bottom: 10px;
}
.wp_slrp_img {
display: block;
max-width: 150px;
max-height: 150px;
width: 100%;
height: auto;
padding: 4px;
background: #fefefe;
border: 1px solid #e5e5e5;
}
.wp_slrp_hidden {
display: none;
}
jQuery(document).ready(function($){
var file_frame;
jQuery('.wp_slrp_upload_button').on('click', function( event ){
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.file_frame = wp.media({
title: jQuery( this ).data( 'uploader_title' ),
button: {
text: jQuery( this ).data( 'uploader_button_text' ),
},
multiple: false
});
file_frame.on( 'select', function() {
attachment = file_frame.state().get('selection').first().toJSON();
jQuery('#wp_slrp_profile_photo_url').val(attachment.url);
jQuery('#wp_slrp_profile_photo_edit_url').val('/wp-admin/post.php?post='+attachment.id+'&action=edit&image-editor');
jQuery('.wp_slrp_img').attr('src', attachment.url).show();
});
file_frame.open();
});
jQuery('.remove_img').on('click', function( event ){
jQuery(this).parent().fadeOut('fast', function(){
jQuery(this).remove();
jQuery('.wp_slrp_img').attr('src', '').hide();
});
jQuery('#wp_slrp_profile_photo_url, #wp_slrp_profile_photo_edit_url').val('');
return false;
});
});
add_action( 'admin_enqueue_scripts', 'user_profile_picture_enqueue_scripts_styles' );
function user_profile_picture_enqueue_scripts_styles() {
wp_enqueue_style( 'user_profile_picture_admin', plugins_url( 'plugins_name/css/custom-admin.css' ), false, '1.0.0', 'all' );
wp_enqueue_script( 'user_profile_picture_admin', plugins_url( 'plugins_name/js/custom-admin.js' ), array('jquery'), '1.0.0', true );
}
add_action( 'show_user_profile', 'user_profile_picture_fields' );
add_action( 'edit_user_profile', 'user_profile_picture_fields' );
function user_profile_picture_fields( $user ) {
if(!current_user_can('upload_files'))
return false;
$wp_slrp_profile_photo_url = get_the_author_meta( 'wp_slrp_profile_photo_url', $user->ID );
if ( empty( $wp_slrp_profile_photo_url ) ) {
$btn_text = 'Upload Profile Photo';
} else {
$wp_slrp_profile_photo_edit_url = get_home_url().get_the_author_meta( 'wp_slrp_profile_photo_edit_url', $user->ID );
$btn_text = 'Change Profile Photo';
}
?>
<h3><?php _e( 'Profile Info', 'wp_slrp' ); ?></h3>
<table class="form-table">
<tr class="user-profile-photo-wrap">
<th><label for="profile-photo"><?php _e( 'Profile Photo', 'wp_slrp' ); ?></label></th>
<td>
<div class="wp_slrp_img_wrap">
<?php if ( $wp_slrp_profile_photo_url ) : ?>
<img src="<?php echo esc_url( $wp_slrp_profile_photo_url ); ?>" class="wp_slrp_img">
<div class="edit_options">
<a href="#" class="remove_img"><span>Remove</span></a>
<a href="<?php echo $cupp_upload_edit_url; ?>" class="edit_img" target="_blank"><span>Edit</span></a>
</div>
<?php else : ?>
<img src="" class="wp_slrp_img wp_slrp_hidden">
<?php endif; ?>
</div>
<div id="cupp_upload">
<input type="hidden" name="wp_slrp_profile_photo_url" id="wp_slrp_profile_photo_url" value="<?php echo esc_url_raw( $wp_slrp_profile_photo_url ); ?>" class="hidden" />
<input type="hidden" name="wp_slrp_profile_photo_edit_url" id="wp_slrp_profile_photo_edit_url" value="<?php echo esc_url_raw( $wp_slrp_profile_photo_edit_url ); ?>" class="hidden" />
<input type='button' class="wp_slrp_upload_button button-primary" value="<?php _e( $btn_text, 'wp_slrp' ); ?>" id="uploadimage"/><br />
</div>
</td>
</tr>
</table>
<?php
wp_enqueue_media();
}
add_action( 'personal_options_update', 'user_profile_picture_save_fields' );
add_action( 'edit_user_profile_update', 'user_profile_picture_save_fields' );
function user_profile_picture_save_fields( $user_id ) {
if ( !current_user_can( 'upload_files', $user_id ) )
return false;
update_user_meta( $user_id, 'wp_slrp_profile_photo_url', $_POST['wp_slrp_profile_photo_url'] );
update_user_meta( $user_id, 'wp_slrp_profile_photo_edit_url', $_POST['wp_slrp_profile_photo_edit_url'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment