0

Hello im trying to add custom meta tags from facebook share button inside image.php file , and i dont know why they dont display, this is how my code looks:

        function add_facebook_image_code(){
            ?>

                <meta property="og:url"           content="http://www.example.com/<?=$post->post_name ?>/" />
                <meta property="og:type"          content="website" />
                <meta property="og:title"         content="example.com - <?=$post->post_title ?>" />
                <meta property="og:image"         content="<?=$post->guid ?>" />

            <?php

        }

        add_action('wp_head', 'add_facebook_image_code');

I add this code inside image.php, is it possible to do it only inside that file ?

Thanks and best regards.

  • Ok i fix the problem, my code was inside loop (i know it was stupid ) it should be on top of page before get_header(); – Mariusz Jan 15 '19 at 0:11
0

Probably because when the execution is hitting your code is already too late to hook on wp_head.

If you want to have your code in image.php, make sure you place it before get_header().

In any case, I would move the code to functions.php or dedicated file and maybe use is_attachment() to conditionally print the meta tags.

  • Thanks i already fix it by myself , but you are right. It should be before get_header() not inside my code :) – Mariusz Jan 15 '19 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.