最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

social sharing - what function can I use to automatically output og tags per pagepost?

programmeradmin0浏览0评论

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
  • Are the og: tags currently generated by another plugin? – Celso Bessa Commented Jan 9, 2021 at 14:46
  • Yes they are. I am using Yoast SEO plugin but the problem is that it inserts the og tags before the closing <head> tag. Which results in not returning any social review for whatsapp and telegram. Also, The tags are shown in the page source but I don't see anything in the header.php. – joseph Commented Jan 9, 2021 at 14:53
  • 1 Then, check @Q Studio response, but you might want to adapt it and modify Yoast own functions, which are hooked using add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );and add_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
Add a comment  | 

1 Answer 1

Reset to default 1

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

    }
发布评论

评论列表(0)

  1. 暂无评论