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="http://www.phpkida.com/" />
<meta property="og:site_name" content="Site Name"/>
<meta property="og:image" content="http://www.phpkida.com/images/image-src.jpg" />

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;
	}
	else
	{
		$image = get_template_directory_uri().'/imges/default-image.jpg';
	}
	
	if($excerpt = $post->post_excerpt)
	{
		$excerpt = strip_tags($post->post_excerpt);
		$excerpt = str_replace("", "'", $excerpt);
	}
	else
	{
		$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; ?>"/>
<?php
}
}
add_action('wp_head', 'fb_open_graph_meta_tags');

I Hope You Like This Article Share With Your Friends.

Leave a Reply

Related Posts: You may like

How to Create and get WordPress Custom Field

How to add post from frontend in wordpress without plugin

WordPress Websites Are SEO Friendly Or Not

How to get last insert id in WordPress

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.