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.
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.