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:
- Install and activate Yoast SEO.
- Go to Yoast SEO → Social.
- Enable the Facebook and Twitter tabs.
- 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.