Created
October 22, 2018 10:10
-
-
Save raazon/e98ac0487344bde98dc9310ef8d8cc18 to your computer and use it in GitHub Desktop.
Add a user avatar using wordpress Media Library.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access pages directly. | |
/* | |
* Load style & scripts | |
*/ | |
function itclanbd_admin_head_scripts( $hook ) { | |
$allowed = ['profile.php', 'user-new.php', 'user-edit.php']; | |
if ( ! in_array( $hook, $allowed ) ) { | |
return; | |
} | |
wp_enqueue_media(); | |
wp_enqueue_style('finance2sell', plugins_url('css/style.css', __FILE__), array(), null); | |
wp_enqueue_script('finance2sell', plugins_url('js/scripts.js', __FILE__), array(), '1.3', true); | |
} | |
add_action( 'admin_enqueue_scripts', 'itclanbd_admin_head_scripts' ); | |
/** | |
* Create fields | |
* @param $user | |
*/ | |
function itclanbd_add_custom_user_profile_fields($user) { | |
$itclanbd_attachment_id = (int)get_user_meta( (int)$user->ID, 'itclanbd_attachment_id', true ); | |
?> | |
<table class="form-table"> | |
<tbody> | |
<tr> | |
<th> | |
<label for="itclanbd-add-media"><?php _e('Avatar', 'finance2sell'); ?></label> | |
</th> | |
<td> | |
<input type="number" name="itclanbd_attachment_id" class="itclanbd-attachment-id" value="<?php echo $itclanbd_attachment_id; ?>" /> | |
<div class="itclanbd-attachment-image"> | |
<?php echo get_avatar($user->ID); ?> | |
</div> | |
<div class="wp-media-buttons"> | |
<button class="button itclanbd-add-media" id="itclanbd-add-media"><?php _e('Select', 'finance2sell'); ?></button> | |
<button class="button itclanbd-remove-media"><?php _e('Remove', 'finance2sell'); ?></button> | |
</div> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
} | |
add_action( 'show_user_profile', 'itclanbd_add_custom_user_profile_fields' ); | |
add_action( 'edit_user_profile', 'itclanbd_add_custom_user_profile_fields' ); | |
add_action( 'user_new_form', 'itclanbd_add_custom_user_profile_fields' ); | |
/** | |
* Update fields | |
* @param $user_id | |
* @return bool | |
*/ | |
function itclanbd_update_custom_user_profile($user_id) { | |
if( !current_user_can('edit_user', (int)$user_id) ) return false; | |
delete_user_meta( (int)$user_id, 'itclanbd_attachment_id') ; //delete meta | |
if( //validate POST data | |
isset($_POST['itclanbd_attachment_id']) | |
&& is_numeric($_POST['itclanbd_attachment_id']) | |
&& $_POST['itclanbd_attachment_id'] > 0 | |
) { | |
add_user_meta( (int)$user_id, 'itclanbd_attachment_id', (int)$_POST['itclanbd_attachment_id'] ); //add user meta | |
} else { | |
return false; | |
} | |
return true; | |
} | |
add_action('user_register', 'itclanbd_update_custom_user_profile'); | |
add_action('profile_update', 'itclanbd_update_custom_user_profile'); | |
//add_action( 'personal_options_update', 'itclanbd_update_custom_user_profile' ); | |
//add_action( 'edit_user_profile_update', 'itclanbd_update_custom_user_profile' ); | |
/** | |
* @param int $attachment_id | |
* @param string $size | |
* @return mixed | |
*/ | |
function itclanbd_get_attachment_url($attachment_id = 0, $size = 'thumbnail') { | |
$image = wp_get_attachment_image_src((int)$attachment_id, $size); | |
return $image[0]; | |
} | |
/** | |
* @param string $avatar | |
* @param $id_or_email | |
* @return mixed|string | |
*/ | |
function itclanbd_get_new_avatar( $avatar = '', $id_or_email ) { | |
$user_id = 0; | |
if ( is_numeric($id_or_email) ) { | |
$user_id = (int)$id_or_email; | |
} else if ( is_string($id_or_email) ) { | |
$user = get_user_by( 'email', $id_or_email ); | |
$user_id = $user->id; | |
} else if ( is_object($id_or_email) ) { | |
$user_id = $id_or_email->user_id; | |
} | |
if ( $user_id == 0 ) return $avatar; | |
$itclanbd_attachment_id = (int)get_user_meta( (int)$user_id, 'itclanbd_attachment_id', true ); | |
if($itclanbd_attachment_id){ | |
$image = itclanbd_get_attachment_url((int)$itclanbd_attachment_id, 'thumbnail'); | |
$avatar = preg_replace('/src=("|\').*?("|\')/i', 'src="'.$image.'"', $avatar); | |
$avatar = preg_replace('/srcset=("|\').*?("|\')/i', 'srcset="'.$image.'"', $avatar); | |
}else{ | |
$avatar = preg_replace('/src=("|\').*?("|\')/i', 'src="'.esc_url( get_avatar_url( (int)$user_id ) ).'"', $avatar); | |
$avatar = preg_replace('/srcset=("|\').*?("|\')/i', 'srcset="'.esc_url( get_avatar_url( (int)$user_id ) ).'"', $avatar); | |
} | |
return $avatar; | |
} | |
add_filter( 'get_avatar', 'itclanbd_get_new_avatar', 5, 5 ); | |
/** | |
* @param $plugin | |
*/ | |
function itclanbd_redirect_after_activation( $plugin ) { | |
if( $plugin == plugin_basename( __FILE__ ) ) { | |
wp_redirect( admin_url('profile.php') ); | |
exit(); | |
} | |
} | |
//add_action( 'activated_plugin', 'itclanbd_redirect_after_activation' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment