php - How to exchange Woocommerce profile photo and WP user avatar

I am using WP User Avatar plugin and Woocommerce Profile Photo plugin together.
To upload avatar from woocommerce edit account front end, I am using Woocommerce Profile Photo Plugin.
https://webfor99.com/woocommerce-custom-profile-picture-avatar-plugin/
And to show more free options for user avatar, I am using WP User Avatar.
But between them no connections now, so I want to use uploaded image by Woocommerce Profile Photo for WP user avatar. Below is what I did.
function change_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$all_users = get_users(array('meta_key' => 'profile_pic',));
foreach ($all_users as $user) {
$user_id = $user->ID;
?><pre><?php var_dump( $user_id ); ?></pre><?php
$profile_pic = get_user_meta($user_id, 'profile_pic', true);
?><pre><?php var_dump( $profile_pic ); ?></pre><?php
update_user_meta($user_id, 'wp_user_avatar', $profile_pic);
}
}
add_action('profile_update', 'change_avatar');
add_action('user_register', 'change_avatar');
Usedupdate_user_meta()
to change avatar image but not working. As you can see I did some debug works to check if it works. Beforeupdate_user_meta()
it works correctly. But user avatar is not changing.
It is getting correct uploaded image ID but avatar is not changing.
Please let me get some more help. Thanks
Answer
Solution:
add_action('profile_update', 'change_avatar');
add_action('user_register', 'change_avatar');
This code will not work. Please useadd_filter('get_avatar', 'change_avatar', 10, 5)
instead.
I tested and it works.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: apache/2.4.52 (win64) openssl/1.1.1m php/8.1.2 server at localhost port 80
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.