Andrew Hudson

Friday, 12 October 2018

How to trigger a webhook with WordPress

I have a Gatsby site andrewhudson.me which is a static Server Side Rendered (SSR) site built using React and hosted on Netlify. I wanted to trigger a build when I published a new post (or updated an existing one) in WordPress and here’s how I did it.

  1. Get webhook url from Netlify
  2. In WordPress code:
    function ah_webhook_netlify_post() {
    	$url = 'https://api.netlify.com/build_hooks/XXXXXXXXXXX';

    	$args =	array(
    		'method' => 'POST',
    		'timeout' => 5,
    		'blocking' => false,
            );
    	wp_remote_post( $url, $args );
    }
    add_action( 'publish_post', 'ah_webhook_netlify_post' );
← Back to all blog posts
Edit this post on Github