Add Facebook meta tag to the header with code snippet or with wordpress plugin

Last update: 16.09.2022.

This code snippet adds Facebook Open Graph meta tags to the header and fills them in automatically, helping you to share on Facebook pages or groups.

Add Facebook meta tag to WordPress head without plugin


<?php add_action('wp_head', 'twd_facebook_meta');function twd_facebook_meta(){?>
<meta property=”og:url” content=”<?php the_permalink() ?>”/>
<meta property=”og:title” content=”<?php single_post_title(”); ?>” />
<meta property=”og:description” content=”<?php if (have_posts() && is_single() OR is_page()):while(have_posts()):the_post();$out_excerpt = str_replace(array(“\r\n”, “\r”, “\n”), “”, get_the_excerpt());echo apply_filters('the_excerpt_rss', $out_excerpt);
endwhile; endif; ?>” />
<meta property=”og:type” content=”article” />
<meta property=”og:image” content=”<?php if ( has_post_thumbnail() ) {the_post_thumbnail_url();} else { ?><?php bloginfo('template_directory'); ?>/screenshot.jpg<?php } ?>” />
<?php };?>
Copy to Clipboard

Related posts

Got a question? Is something not working? Write and I'll answer!

Leave a Reply

Your email address will not be published. Required fields are marked *

ContactOur Works