How to Add OG Images to WordPress — No Plugin Required
Add dynamic Open Graph images to WordPress without a plugin. Covers functions.php, Yoast SEO, Rank Math, custom themes, and API-based OG image generation.
Quick Answer
To add OG images to WordPress without a plugin, add a wp_head action in your theme's functions.php that outputs og:image meta tags using the post title. For a plugin-based approach, Yoast SEO and Rank Math both support custom OG images. The fastest automated method is pointing the og:image to an API like ogimg.xyz.
Method 1: functions.php (No Plugin)
The simplest way to add OG images to WordPress without installing any plugin is to hook into wp_head from your theme's functions.php:
// functions.php — Add this to your active theme
function custom_og_image_tags() {
$title = is_singular() ? get_the_title() : get_bloginfo('name');
$description = is_singular() ? get_the_excerpt() : get_bloginfo('description');
$url = is_singular() ? get_permalink() : home_url('/');
// Generate OG image via ogimg.xyz API
$og_image = 'https://ogimg.xyz/api/og?' . http_build_query([
'title' => $title,
'description' => wp_trim_words($description, 20, '...'),
'template' => 'gradient',
]);
echo '<meta property="og:type" content="' . (is_singular() ? 'article' : 'website') . '" />' . "
";
echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "
";
echo '<meta property="og:description" content="' . esc_attr($description) . '" />' . "
";
echo '<meta property="og:url" content="' . esc_url($url) . '" />' . "
";
echo '<meta property="og:image" content="' . esc_url($og_image) . '" />' . "
";
echo '<meta property="og:image:width" content="1200" />' . "
";
echo '<meta property="og:image:height" content="630" />' . "
";
echo '<meta name="twitter:card" content="summary_large_image" />' . "
";
}
add_action('wp_head', 'custom_og_image_tags');
This approach works with any WordPress theme and requires no plugins. Every post and page automatically gets a unique OG image generated from its title and excerpt.
Method 2: Yoast SEO / Rank Math Integration
If you already use Yoast SEO or Rank Math, you can override the default OG image with an API-generated one using a filter:
Yoast SEO
// functions.php — Override Yoast's OG image
function override_yoast_og_image($image) {
if (is_singular()) {
$title = get_the_title();
$description = get_the_excerpt();
$image = 'https://ogimg.xyz/api/og?' . http_build_query([
'title' => $title,
'description' => wp_trim_words($description, 20, '...'),
'template' => 'blog',
]);
}
return $image;
}
add_filter('wpseo_opengraph_image', 'override_yoast_og_image');
Rank Math
// functions.php — Override Rank Math's OG image
function override_rankmath_og_image($image) {
if (is_singular()) {
$title = get_the_title();
$image = 'https://ogimg.xyz/api/og?' . http_build_query([
'title' => $title,
'template' => 'gradient',
]);
}
return $image;
}
add_filter('rank_math/opengraph/facebook/image', 'override_rankmath_og_image');
add_filter('rank_math/opengraph/twitter/image', 'override_rankmath_og_image');
This lets you keep Yoast or Rank Math for other SEO features while using API-generated OG images that always match your post titles.
Method 3: Custom Theme with Featured Image Fallback
For a more robust solution, create a helper function that tries the featured image first, then falls back to the API:
// functions.php — Smart OG image with fallback chain
function smart_og_image() {
if (!is_singular()) {
// Home / archive — use site-level OG image
$title = get_bloginfo('name');
return 'https://ogimg.xyz/api/og?' . http_build_query([
'title' => $title,
'template' => 'gradient',
]);
}
// 1. Check for custom field "og_image"
$custom = get_post_meta(get_the_ID(), 'og_image', true);
if ($custom) return $custom;
// 2. Use featured image if set
$thumbnail = get_the_post_thumbnail_url(get_the_ID(), 'full');
if ($thumbnail) return $thumbnail;
// 3. Fall back to API-generated image
return 'https://ogimg.xyz/api/og?' . http_build_query([
'title' => get_the_title(),
'description' => wp_trim_words(get_the_excerpt(), 15, '...'),
'template' => 'blog',
'pattern' => 'dots',
]);
}
function output_smart_og_tags() {
$image = smart_og_image();
echo '<meta property="og:image" content="' . esc_url($image) . '" />' . "
";
echo '<meta property="og:image:width" content="1200" />' . "
";
echo '<meta property="og:image:height" content="630" />' . "
";
}
add_action('wp_head', 'output_smart_og_tags');
This gives content editors full control: they can set a custom OG image per post, let the featured image be used, or rely on automatic generation.
Testing OG Images on WordPress
After adding your OG image code, verify it works correctly:
- View page source: Visit any post on your site, right-click, and select "View Page Source." Search for
og:imageto confirm the meta tag is present. - Check for duplicates: If you're using both a functions.php approach and a plugin like Yoast, you may get duplicate OG tags. Pick one method and stick with it.
- Facebook Sharing Debugger: Paste your post URL at
developers.facebook.com/tools/debug. Click "Scrape Again" if you see stale data. - Twitter Card Validator: Test at
cards-dev.twitter.com/validator. - LinkedIn Post Inspector: Test at
linkedin.com/post-inspector.
Common WordPress issues:
- Caching plugins: If you use WP Super Cache, W3 Total Cache, or similar, purge the cache after adding OG tags so the new HTML is served.
- Duplicate meta tags: Disable OG tag output in your SEO plugin if you're adding tags via functions.php.
- CDN caching: Cloudflare or other CDNs may serve a cached version. Purge the CDN cache after making changes.
Frequently Asked Questions
Do I need a plugin like Yoast just for OG images?
No. The functions.php approach shown above adds OG images without any plugin. Yoast and Rank Math are useful for broader SEO features, but they are not required just for Open Graph tags.
Will this work with WooCommerce product pages?
Yes. The functions.php approach works with any WordPress post type, including WooCommerce products. Use get_the_title() for the product name and get_the_excerpt() for the short description to generate product-specific OG images.
How do I prevent duplicate OG tags if I use Yoast?
If you add OG tags via functions.php while Yoast is active, you'll get duplicates. Either use the Yoast filter (wpseo_opengraph_image) to override Yoast's image, or disable Yoast's social features in Yoast > Social > Facebook tab.
Related Guides
Compare with Alternatives
See how OG Image Generator stacks up against other tools.
Start Generating OG Images
Free API for developers. 50 images/month, 10 templates, no credit card required.