How to Add Og Tags in WordPress Without a Plugin

How to Add Og Tags in WordPress Without a Plugin

Last modified: April 29, 2022

FAQ
Cloudways

Are you looking to add Open Graph meta tags to your WordPress website without the use of one of the numerous SEO plugins? This is a simple process and there are some benefits to it. So in this article, we look into how to add OG tags in WordPress without a plugin.

Show More

* This button will show the rest of the post and open up an offer from a vendor

What are OG Tags?

Open Graph, or OG, Tags are the metadata tags that are used by Facebook and other social media channels to display information about your posts. When someone shares a post on their feed, the social media platform pulls this data to give a more detailed preview of the page.

The advantage of the OG tags is that you can optimize them for each individual platform. Therefore, you can improve the results that you can get from social shares and more.

There are numerous ways that you can get these to be customized using plugins. But that isn’t always the best option. For instance, some plugins only cater for certain platforms like Facebook or Twitter.

Or there might be a conflict between your plugin and the social media channel.

Therefore, you might want to consider making the change yourself through your theme files.

Step 1: Backup

The first step is to ensure that you backup your website. You’re going to be making changes to the code and a simple error can cause lots of problems on your website.

Step 2: Functions

Now you need to access your functions.php file in a text editing document. This can be accessed from your FTP file.

Step 3: Copy and Paste

Once you’ve opened your functions.php file, you need to paste the code below into it.

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'add_opengraph_doctype');

//Let's add Open Graph Meta Info

function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;
echo '';
echo '';
echo '';
echo '';
echo '';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
echo '';
}
else{
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '';
}
echo "
";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

There are several customizations that you will need to do to the code above. For instance, you need to change the site name tag, the default image URL and you will also need to ensure that you add your own Facebook app ID.

If you do not have a Facebook App ID, you can remove that line from the code.

It is advisable to change the image to something with your logo. Therefore, if there is no thumbnail available, at least your brand’s logo is there.

Step 4: Save

Now you need to save the functions.php file.

Step 5: Upload

Once saved, you can upload your functions.php file back up to your website’s FTP directory. This should take just a few seconds to complete.

Final Word: How to Add Og Tags in WordPress Without a Plugin

Above are the simple instructions on how to add OG tags in WordPress without a plugin. It is simple, but you need to ensure that you save your website’s backup before you make the changes, in case it doesn’t work.

Recap:
Save 4 Later
Email liked links to yourself