Yoast SEO plugin open graph add meta tag to fetch Avatar from Author Page
-
10-04-2021 - |
题
I'm trying to create a new og:image for author avatar when the users sharing their author profile page to Facebook.
I'm a newbie with WordPress, After searching for more than 6 months but i'm always failing.
My site is multi-authors and all what i need to add og:image meta tag for author.php "author profile", I'm stuck and I really get tired from searching.
When our users trying to share his profile..the avatar image not coming up! because Facebook can't find the author avatar og:image!
Plugins i'm using : Yoast SEO, Simple Local Avatars, AddToAny Share Buttons.
I tried to use wpseo_opengraph_image filter, but not working.
This is code I'm using:
function custom_author_og_image() {
if ( is_author ( ) ) {
$author = get_queried_object();
$image_attributes = get_avatar_url( $author->user_email, 'full' );
if ( $image_attributes !== false ) {
return $image_attributes[0];
}
}
}
add_filter('wpseo_opengraph_image', 'custom_author_og_image', 10, 0);
没有正确的解决方案
其他提示
did you tried to check if any og:image duplicate in your <head>
tag.
Or you can try to increase the priority of filter hook. Example below, using the filter with priority = 99 (almost latest).
add_filter('wpseo_opengraph_image', 'custom_author_og_image', 99);
Finally i found the answer and i would like to sharing it with you, but i have another small problem. Now i can get the author avatar IF he has 1 post at least!! and the authors with 0 posts Facebook can't fetch his avatar image!! Strange thing and i don't know how to fix this.
anyway this is the code i found.
add_action('wpseo_head', 'add_fb_og', 5);
function add_fb_og() {
if ( is_author ( ) ) {
$my_custom_avatar = get_avatar(get_the_author_meta( 'ID' ), 200 );
$document = new DOMDocument();
@$document->loadHTML($my_custom_avatar);
$nodes = $document->getElementsByTagName('img');
?>
<meta property="og:image" content="<?php echo $nodes->item(0)->getAttribute('src'); ?>" />
<?php
}
}