I have a feature that allows me to upload images from the settings section of the woocommerce account. The function comes from here: Add a profile picture (file upload) on My account > edit account in WooCommerce
I have a feature that allows me to upload images from the settings section of the woocommerce account. The function comes from here:
Everything works correctly, however there is a small drawback. The avatar is not applied to the comments section of posts or product reviews.
So since the function is updating the data of the image metakey, I was thinking of setting this metakey as the universal value from which wordpress has to get the image url. This way I believe that the uploaded image is also displayed in the comments and reviews sections.
Can anyone help me with this ?
Here's what I'm trying to do, but it doesn't work:
The code below I got it from: https://gist.github.com/tripflex/3190b9b3b484f99c0a4ba4f6c59cb44f
<?php
add_filter( 'pre_get_avatar_data', 'smyles_set_avatar_based_on_user_meta', 10, 2 );
function smyles_set_avatar_based_on_user_meta( $args, $id_or_email ){
// Set this to the full meta key set in Save As under Auto Populate tab (for WP Job Manager Field Editor)
$user_avatar_meta_key = 'image';
// Check for comment_ID
if( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ){
$id_or_email = get_comment( $id_or_email );
}
// Check if WP_Post
if( $id_or_email instanceof WP_Post ){
$user_id = $id_or_email->post_author;
}
// Check if WP_Comment
if( $id_or_email instanceof WP_Comment ){
if( ! empty( $id_or_email->user_id ) ){
$user_id = $id_or_email->user_id;
} elseif( ! empty( $id_or_email->comment_author_email ) ){
// If user_id not available, set as email address to handle below
$id_or_email = $id_or_email->comment_author_email;
}
}
if( is_numeric( $id_or_email ) ){
$user_id = $id_or_email;
} elseif( is_string( $id_or_email ) && strpos( $id_or_email, '@' ) ){
$id_or_email = get_user_by( 'email', $id_or_email );
}
// Last check, convert user object to ID
if( $id_or_email instanceof WP_User ){
$user_id = $id_or_email->ID;
}
// Now that we have a user ID, check meta for avatar file
if( ! empty( $user_id ) && is_numeric( $user_id ) ){
$meta_val = maybe_unserialize( get_user_meta( $user_id, $user_avatar_meta_key, TRUE ) );
if( ! empty( $meta_val ) ){
// Set to first value if returned value is array
if( is_array( $meta_val ) && ! empty( $meta_val[0] ) ){
$meta_val = $meta_val[0];
}
// As long as it's a valid URL, let's go ahead and set it
if( filter_var( $meta_val, FILTER_VALIDATE_URL ) ){
$args['url'] = $meta_val;
}
}
}
return $args;
}
source https://stackoverflow.com/questions/73378485/how-to-set-image-url-from-users-meta-value-as-wordpress-avatar-throughout-entir
No comments:
Post a Comment