Do you want to add Facebook Open Graph meta data to your WordPress themes?
Open Graph metadata helps Facebook and other social media websites get meta data about your posts pages. It also allows you to control how your content appears when shared on Facebook.
In this article, we will show you how to easily add Facebook open graph metadata in WordPress themes. Weβll share three different methods, so you can choose one that works best for you.
Method 1. Adding Facebook Open Graph Meta Data with AIOSEO
All in One SEO is a popular WordPress SEO plugin used by over 2 million websites. It allows you to easily optimize your website for search engines as well as social platforms like Facebook and Twitter.
First, you need to install and activate the All in One SEO plugin. For more details, see our step by step guide on how to install a WordPress plugin.
Upon activation, you need to visit All in One SEO Β» Social Networks page. Here you can enter your Facebook page URL and all your other social networks.

Next, click on the Facebook tab at the top of the page, and youβll see that Open Graph Markup is enabled by default. You can click the βUpload or Select Imageβ button to choose a default Facebook OG image if an article doesnβt have an open graph image.

If you scroll down, you can customize your site name, description, and more settings. Donβt forget to click the blue Save button once youβre done making changes.
Now that you have set site-wide open graph meta tags, the next step is to add open graph meta data for individual posts and pages.
By default, AIOSEO will use your post title and description for open graph title and description. You can also manually set the Facebook thumbnail for each page and post.
Simply edit the post or page and scroll down to the All in One SEO section below the editor. From here, switch to the Social tab and youβll see a preview of your thumbnail.

You can set the social media image here as well as title and description. Just scroll down to the Image Source field. You can choose to use the featured image, upload a custom image, or other options.

Method 2. Set Facebook Open Graph Meta Data using Yoast SEO
Yoast SEO is another excellent WordPress SEO plugin that you can use to add Facebook open graph meta data into any WordPress site.
First thing you need to do is install and activate, the Yoast SEO plugin. For more details, see our step by step guide on how to install a WordPress plugin.
Once activated, you need to go to SEO Β» Social and simply check the box next to Add Open Graph meta data.

You can save your settings or continue and configure other Facebook social options on the screen.
You can provide a Facebook app ID if you use one for your Facebook page and insights. You can also change your homepage Open Graph meta title, description, and image.
Lastly, you can set a default image to be used when no image is set for a post or page.
Yoast SEO also allows you to set Open Graph metadata for individual posts and pages. Simply edit a post or page and scroll down to the SEO section below the editor.

From here, you can set Facebook thumbnail for that particular post or page. If you donβt set a post title or description, then the plugin will use your SEO meta title and description.
You can now save your post or page and the plugin will store your Facebook open graph meta data.
Method 3. Manually Add Facebook Open Graph Meta Data into Your WordPress Theme
This method requires you to edit your theme files, so make sure that you back up your theme files before making any changes.
After that simply copy and paste this code in your themeβs functions.php file, or in a site-specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //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' ); //Lets 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 '<meta property="fb:app_id" content="Your Facebook App ID" />' ; echo '<meta property="og:title" content="' . get_the_title() . '"/>' ; echo '<meta property="og:type" content="article"/>' ; echo '<meta property="og:url" content="' . get_permalink() . '"/>' ; echo '<meta property="og:site_name" content="Your Site NAME Goes HERE"/>' ; 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 '<meta property="og:image" content="' . $default_image . '"/>' ; } else { $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post ->ID ), 'medium' ); echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src [0] ) . '"/>' ; } echo " "; } add_action( 'wp_head' , 'insert_fb_in_head' , 5 ); |
Note: Remember to change the Site Name where it says βYour Site Name Goes Hereβ. After that, change the default image URL with the image of yours. You also need to add your own Facebook app ID, If you donβt have a Facebook app, then you can remove the Facebook app ID line from the code.
We would recommend putting an image with your logo there, so if your post does not have a thumbnail, then it pulls your siteβs logo.
Thatβs all you need to do. As soon as you save your functions.php file (or site-specific plugin) it will start showing Facebook open graph metadata in the WordPress header.
We hope this article helped you add Facebook open graph meta data in WordPress. You may also want to see our pick of the best social media plugins for WordPress to grow your social following, and our troubleshooting guide on how to fix the Facebook incorrect thumbnail issue in WordPress.
If you liked this article, then please subscribe to our YouTube Channel for more WordPress video tutorials. You can also find us on Twitter and Facebook.