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'] ) ); ?>" />
<?
}
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