SwarmGen+WordPress

SwarmGen + WordPress

Generate featured images and social cards from every post

WordPress powers over 40% of the web. With a small snippet of code in your theme's functions.php, or a lightweight plugin, you can hook into the publish_post action and automatically request a SwarmGen render every time a new post goes live. The rendered image URL can be stored as post meta and referenced in your theme or SEO plugin.

How to set it up

  1. 1

    Create a SwarmGen template

    Build an HTML template with merge fields for the data you want to pull from WordPress — {{title}}, {{excerpt}}, {{category}}, {{featured_image}}.

  2. 2

    Hook into WordPress post publishing

    Add a transition_post_status action hook in functions.php. When a post transitions to "publish", fire a wp_remote_post() call to the SwarmGen render API.

  3. 3

    Pass post data as merge fields

    Extract the post title, excerpt, featured image URL, and any other fields from the WP_Post object and include them in the SwarmGen request body.

  4. 4

    Store the image URL as post meta

    Once rendering is complete (via webhook callback), use update_post_meta() to save the returned image URL against the post. Reference it in templates or your Yoast/RankMath OG settings.

Code example

WordPress functions.php — publish hookphp
add_action('transition_post_status', function($new, $old, $post) {
    if ($new !== 'publish' || $old === 'publish') return;
    if ($post->post_type !== 'post') return;

    $featured_url = '';
    if (has_post_thumbnail($post->ID)) {
        $featured_url = get_the_post_thumbnail_url($post->ID, 'large');
    }

    $response = wp_remote_post('https://swarmgen.io/api/templates/YOUR_TEMPLATE_UUID/render', [
        'headers' => [
            'Content-Type'  => 'application/json',
            'Authorization' => 'Bearer ' . (defined('SWARMGEN_API_KEY') ? SWARMGEN_API_KEY : ''),
        ],
        'body' => json_encode([
            'data' => [
                'title'    => get_the_title($post->ID),
                'excerpt'  => get_the_excerpt($post->ID),
                'category' => get_the_category($post->ID)[0]->name ?? '',
                'image'    => $featured_url,
            ],
            'returnUrl' => true,
        ]),
    ]);
    $body = json_decode(wp_remote_retrieve_body($response), true);
    update_post_meta($post->ID, '_swarmgen_image_url', $body['url'] ?? '');
}, 10, 3);

Bespoke integrations are not supported by our team. The sample code here is for guidance and you will need technical knowledge or a developer to implement it. We plan to release dedicated plugins for key tools as demand grows.

Ready to connect WordPress?

Create a free account, build a template, and start generating images from your WordPress workflows in minutes.

Get started free

More integrations