How to Add Open Graph Meta Data in WordPress Without using plugin

How to Add Open Graph Meta Data in WordPress Without using plugin

In this article or tutorial we are sharing with you how to set open graph (OG) meta data in WordPress without using plugin.

What Is Open Graph Meta Tags

The Open Graph meta tags you can add to your web page to allow to specify Facebook, google, twitter title, description and set image that is used by social media platforms. In technical term terms, this means you can use basic HTML tags to add key information about your posts, web page, which share on social networks like Facebook, Twitter, LinkedIn and Google+ use to enhance how links to your site are displayed. If you have want share with Facebook or set a custom thumbnail, title and description called, then you have need to use open graph tags in action. You can do this by adding a function to your theme function.php file, or by using a plugin that can do the heavy lifting for you. But in this article, we’ll we just talk about function.php file means costume code.

How Open Graph Works

You need to add a few meta tags to the head section of your HTML document or in webpage. This is storing information about your web page , but not visible to users. Each meta tag follows the same format; you assign a “property” a “content” value. After you have done this. you can use for social media platforms, you can use this information to display your content in a more meaningful way. you can set custom Title, description, or thumbnail.

Here we are using some Open Graph Tags

<meta property="og:title" content="Article Title" />
<meta property="og:description" content="Article Description (One or Two sentences)" />
<meta property="og:type" content="article" />
<meta property="og:url" content="" />
<meta property="og:site_name" content="Site Name"/>
<meta property="og:image" content="" />

Add Open Graph Tags for Every Post

function fb_open_graph_meta_tags() {
if (is_single())
  global $post;
 	if(get_the_post_thumbnail($post->ID, 'thumbnail'))
		$thumbnail_id = get_post_thumbnail_id($post->ID);
		$thumbnail_object = get_post($thumbnail_id);
		$image = $thumbnail_object->guid;
		$image = get_template_directory_uri().'/imges/default-image.jpg';
	if($excerpt = $post->post_excerpt)
		$excerpt = strip_tags($post->post_excerpt);
		$excerpt = str_replace("", "'", $excerpt);
		$excerpt = get_bloginfo('description');

<meta property="og:title" content="<?php echo the_title(); ?>"/>
<meta property="og:description" content="<?php echo $excerpt; ?>"/>
<meta property="og:type" content="article"/>
<meta property="og:url" content="<?php echo the_permalink(); ?>"/>
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
<meta property="og:image" content="<?php echo $img_src; ?>"/>
add_action('wp_head', 'fb_open_graph_meta_tags');

I Hope You Like This Article Share With Your Friends.

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.