Fix Guide

How to Add Open Graph Tags in WordPress

February 8, 2026

Open Graph tags control how your content appears in social media previews. Without them, platforms guess the title, description, and image — often incorrectly.



Method 1: Yoast SEO (Recommended)


Yoast SEO automatically generates Open Graph and Twitter Card tags based on your content:



  1. Install and activate Yoast SEO.

  2. Go to Yoast SEO → Social.

  3. Enable the Facebook and Twitter tabs.

  4. Set default images for pages without a featured image.


For individual posts, use the "Social" tab in the Yoast meta box to customize the title, description, and image for social sharing.



Method 2: Manual Implementation


Add Open Graph tags via functions.php:


function add_open_graph_tags() {
    if (is_singular()) {
        global $post;
        $title = get_the_title();
        $description = has_excerpt() ? get_the_excerpt() : wp_trim_words($post->post_content, 30);
        $url = get_permalink();
        $image = get_the_post_thumbnail_url($post->ID, 'large');

        echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "\n";
        echo '<meta property="og:description" content="' . esc_attr($description) . '" />' . "\n";
        echo '<meta property="og:url" content="' . esc_url($url) . '" />' . "\n";
        echo '<meta property="og:type" content="article" />' . "\n";
        if ($image) {
            echo '<meta property="og:image" content="' . esc_url($image) . '" />' . "\n";
        }
    }
}
add_action('wp_head', 'add_open_graph_tags');


Best Practices



  • Use images at least 1200x630 pixels for best display on Facebook

  • Keep og:title under 60 characters

  • Keep og:description under 200 characters

  • Always set a featured image for every post



Verify with InspectWP


Run an InspectWP scan to verify your Open Graph tags are present and correctly configured.

Check your WordPress site now

InspectWP analyzes your WordPress site for security issues, SEO problems, GDPR compliance, and performance — for free.

Analyze your site free