Headless WordPress · Lesson 22 of 22 · 20 min

Go live and rebuild on publish

Point Vercel at a hosted WordPress, then make every publish in WordPress trigger a fresh deploy.

Your laptop WordPress can’t feed a live site. Vercel builds in the cloud, and the cloud can’t reach 127.0.0.1 on your computer. For production you need WordPress somewhere public.

1. Get a hosted WordPress

Any host running regular WordPress works, as long as /wp-json/wp/v2/posts loads in a browser. A common setup is putting it on a subdomain:

  • sundaysupper.com → Astro on Vercel (what visitors see)
  • cms.sundaysupper.com → WordPress (where editors write)

Move your practice content over. In Playground, go to Tools → Export, choose “All content”, and download the file. On the hosted site, go to Tools → Import → WordPress, install the importer if asked, and upload the file. Check “Download and import file attachments” so the images come too.

2. Tell Vercel where WordPress lives

Your .env file isn’t in git, so Vercel doesn’t have it. Add the variable in Vercel:

  1. Open your project on Vercel and go to Settings → Environment Variables
  2. Name: WORDPRESS_URL
  3. Value: https://cms.sundaysupper.com (your real address, no trailing slash)
  4. Save

Merge your branch and push:

git checkout main
git merge wordpress
git push

Vercel builds, the loader fetches from your hosted WordPress, and the live site now shows WordPress content.

3. Rebuild when you publish

Right now a new WordPress post only appears after the next deploy. Fix that with a deploy hook: a secret URL that starts a Vercel build when anything sends a request to it.

Create the hook in Vercel:

  1. Go to Settings → Git → Deploy Hooks
  2. Name it wordpress, branch main, and click Create Hook
  3. Copy the URL. It looks like https://api.vercel.com/v1/integrations/deploy/prj_.../...

Test it from your terminal:

curl -X POST "YOUR_DEPLOY_HOOK_URL"

A new deployment appears in Vercel.

Call the hook from WordPress. Store the URL in wp-config.php on your WordPress server, above the line that says “That’s all, stop editing!”:

wp-config.php

define( 'VERCEL_DEPLOY_HOOK', 'https://api.vercel.com/v1/integrations/deploy/prj_xxx/yyy' );

Then add a small plugin. Create this file in wp-content/mu-plugins/ (make the folder if it doesn’t exist). “Must-use” plugins in that folder run automatically and can’t be switched off by accident.

wp-content/mu-plugins/vercel-deploy.php

<?php
/**
 * Plugin Name: Vercel Deploy on Publish
 * Description: Rebuilds the Astro site on Vercel when a post or page is published, updated, or unpublished.
 */

add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) {
    if ( ! defined( 'VERCEL_DEPLOY_HOOK' ) ) {
        return;
    }

    if ( ! in_array( $post->post_type, array( 'post', 'page' ), true ) ) {
        return;
    }

    // Only care when something is, or was, live.
    if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
        return;
    }

    if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) {
        return;
    }

    wp_remote_post( VERCEL_DEPLOY_HOOK, array(
        'blocking' => false,
        'timeout'  => 5,
    ) );
}, 10, 3 );

What the plugin does

  • transition_post_status fires every time a post’s status changes, including updates to a published post (publish to publish).
  • It only reacts to posts and pages, and only when something is or was published. Saving a draft does nothing. Unpublishing a post does rebuild, so it disappears from the live site.
  • 'blocking' => false sends the request without making the editor wait for Vercel to answer.
  • The URL lives in wp-config.php, not the plugin, so the plugin file is safe to share.

Publish a post in WordPress and watch a new deployment start in Vercel. About a minute later, it’s live.