Is it possible to change the user avatar in WordPress programmatically? I’m asking because I’m facing a problem right now in displaying the user avatar in WordPress multisite: the Avatar is not displaying.
Leave a Reply
You must be logged in to post a comment.
I had to do three things to be able to programmatically insert user avatars into my WordPress starting with an avatar that is hosted at a remote URL.
Suppose you have a user whose avatar is
$avatar_url = 'http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a';
I use the
upload_product_image()
from WooCommerce’sclass-wc-api-products.php
to get the avatar into my local server.Then, using some of the code from this support post, create an attachment.
Then associate the attachment with the user.
This works only with the WP User Avatar plugin.
From WooCommerce’s class-wc-api-products.php
Most likely somewhere the
get_avatar
filter is being called and doing something. I recommend searching your plugins and themes forget_avatar
and looking at things that look like:add_filter ('get_avatar', .....
Otherwise, you can write your own behavior with the code below.
This will works :
If you have avatar dir location metas for users, then use this for all users :
Hope you get the point.
first add author_pic meta to user profile:
and add this filter to template function: