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 track contact form 7 form in salesforce without plugin

Contact Form 7 Redirect to Thank You Page

How to Validation Business Email in Contact Form 7 WordPress

How to Create and get WordPress Custom Field

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.