Skip to content

Instantly share code, notes, and snippets.

@mindmergedesign
Forked from freddiemixell/netlify.php
Created January 28, 2019 17:03
Show Gist options
  • Save mindmergedesign/0a7373058598763f99a9fe999d69112f to your computer and use it in GitHub Desktop.
Save mindmergedesign/0a7373058598763f99a9fe999d69112f to your computer and use it in GitHub Desktop.
WordPress Netlify Build Hook
<?php
// Netlify Webhook Build Function
// -- this function will trigger a rebuild on post update
// -- add your api key or url below
add_action( 'save_post', 'netlify_rebuild');
function netlify_rebuild( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( get_post_status( $post_id ) == 'publish' ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.netlify.com/build_hooks/YOUR-API-KEY');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
} else {
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment