← buildlog og

Dynamic OG images for WordPress

Hook into wp_head (or replace the social image filter of your SEO plugin). The API returns a 1200×630 PNG, cached at the edge.

// functions.php
add_action('wp_head', function () {
  if (!is_singular()) return;
  $url = 'https://og.sadana.app/api/og?' . http_build_query([
    'title' => get_the_title(), 'subtitle' => wp_strip_all_tags(get_the_excerpt()), 'site' => parse_url(home_url(), PHP_URL_HOST)
  ]);
  echo '<meta property="og:image" content="' . esc_url($url) . '">';
});

If Yoast or Rank Math already outputs og:image, disable its image or use its filter (e.g. wpseo_opengraph_image) instead, otherwise you get two og:image tags.

Preview

Example OG image for WordPress

Parameters

title     required, up to 200 chars
subtitle  optional
site      optional footer text
theme     light | dark (Pro)
key       Pro license key (removes watermark)

Check it

After deploying, paste a URL into a card validator (e.g. opengraph.xyz) or share it in a private Slack/Discord channel. Social crawlers cache images, so change the URL (e.g. add &v=2) to force a refresh.

Try the generator

Other frameworks

Next.js · Astro · Hugo · WordPress · Ghost · SvelteKit · Nuxt · Eleventy (11ty) · Jekyll · Docusaurus · plain HTML