admin管理员组文章数量:1307608
The issue: I had problem with my links social preview on WhatsApp and Telegram. The social preview appears on desktop and other platforms, also the Facebook debugging tool shows complete information, but it didn't show anything on WhatsApp and Telegram on phone. I did research and tested different stuff and finally found out that WhatsApp and Telegram don't like it when there is a lot of code before them in the <head>
. I brought them closer, and it almost worked, but not completely.
Now, it doesn't show the thumbnail; and worse, it shows the same tags for all of my pages. For example I share a post's link on WhatsApp, but the og tags outputted belongs to my homepage and it doesn't matter which page or post I share, it shows the same og tags.
Now, I almost know what the problem is, and I am looking to test my next hypothesis to see if it works.
I am thinking of making a custom function like this:
I create a function inside functions.php
and include all the og tags in it. Then, put a single line of code "for example", <?(get_og_tags);?>
inside the <head>
. Exactly after the opening tag hoping it would work.
My first question:
1- is it a good decision though?
2- If it is, can you help me with the function that does this?
I am looking to resolve this issue and I have no insist by solving it with function. Using a function is just what came to my mind. I'll appreciate if you can help me with whatever solution you know that work.
The issue: I had problem with my links social preview on WhatsApp and Telegram. The social preview appears on desktop and other platforms, also the Facebook debugging tool shows complete information, but it didn't show anything on WhatsApp and Telegram on phone. I did research and tested different stuff and finally found out that WhatsApp and Telegram don't like it when there is a lot of code before them in the <head>
. I brought them closer, and it almost worked, but not completely.
Now, it doesn't show the thumbnail; and worse, it shows the same tags for all of my pages. For example I share a post's link on WhatsApp, but the og tags outputted belongs to my homepage and it doesn't matter which page or post I share, it shows the same og tags.
Now, I almost know what the problem is, and I am looking to test my next hypothesis to see if it works.
I am thinking of making a custom function like this:
I create a function inside functions.php
and include all the og tags in it. Then, put a single line of code "for example", <?(get_og_tags);?>
inside the <head>
. Exactly after the opening tag hoping it would work.
My first question:
1- is it a good decision though?
2- If it is, can you help me with the function that does this?
I am looking to resolve this issue and I have no insist by solving it with function. Using a function is just what came to my mind. I'll appreciate if you can help me with whatever solution you know that work.
Share Improve this question edited Jan 9, 2021 at 20:23 Celso Bessa 1,1288 silver badges18 bronze badges asked Jan 9, 2021 at 9:23 josephjoseph 135 bronze badges 3 |1 Answer
Reset to default 1This is an example how to add some OG tags for Facebook:
Create a hook to a function on the wp_head
action,
// if on a single post screen, generate and insert facebook:OG tags. ##
add_action( 'wp_head', 'wpse381199_og_tags', 12 );
Then create a function to generate the OG tags
function wpse381199_og_tags(){
// get the current post object ##
$the_post = get_post();
// bulk if no post
if ( ! $the_post ){ return false; }
// check we are on a single post or page, if not bulk ##
if (
! \is_single()
&& ! \is_page()
) {
return false;
}
// get all the data we need. ##
$array = [];
$array['title'] = $the_post->post_title;
// add all other tag elements you want to array here and add a new tag for each value in the HTML below.
?>
<meta property="og:title" content="<?php echo ( \esc_html( $array['title'] ) ); ?>" />
<?
}
本文标签: social sharingwhat function can I use to automatically output og tags per pagepost
版权声明:本文标题:social sharing - what function can I use to automatically output og tags per pagepost? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741847430a2400862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );
andadd_action( 'wpseo_head', [ $this, 'present_head' ], -9999 );
. I won't go deeper on because third party-plugins are off-topic here, but the idea is basically what Q_Studio answered, just adapting for different contexts. – Celso Bessa Commented Jan 9, 2021 at 14:58