Home › Forums › Backend Issues (wp-admin) › Update User Profile Image
I’ve been able to create a user form and have it displayed for each user when logged in. The form includes an image upload i’d like to use for the profile image. How would I be able update the profile image for the user when they submit their image on the form?
i have tried to use this, but it is not working for me
/**
* Use ACF image field as avatar
* @author Mike Hemberger
* @link http://thestizmedia.com/acf-pro-simple-local-avatars/
* @uses ACF Pro image field (tested return value set as Array )
*/
add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = '';
// Get user by id or email
if ( is_numeric( $id_or_email ) ) {
$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );
} elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
$user = get_user_by( 'email', $id_or_email );
}
if ( ! $user ) {
return $avatar;
}
// Get the user id
$user_id = $user->ID;
// Get the file id
$image_id = get_user_meta($user_id, 'counselor_image', true);
// Bail if we don't have a local avatar
if ( ! $image_id ) {
return $avatar;
}
// Get the file size
$image_url = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
// Get the file url
$avatar_url = $image_url[0];
// Get the img markup
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
}
This is the code I am using to get updates from the user. It produces a shortcode on my /profile page for the user to make changes. I guess I would need to add something here that would make it replace the user avatar on submit?
function my_acf_user_form_func( $atts ) {
$a = shortcode_atts( array(
'field_group' => ''
), $atts );
$uid = get_current_user_id();
if ( ! empty ( $a['field_group'] ) && ! empty ( $uid ) ) {
$options = array(
'post_id' => 'user_'.$uid,
'field_groups' => array( intval( $a['field_group'] ) ),
'return' => add_query_arg( 'updated', 'true', get_permalink() )
);
ob_start();
acf_form( $options );
$form = ob_get_contents();
ob_end_clean();
}
return $form;
}
add_shortcode( 'my_acf_user_form', 'my_acf_user_form_func' );
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.