Created
January 21, 2020 13:13
-
-
Save g00glen00b/94423e94d115e2b797c1343375bc01a4 to your computer and use it in GitHub Desktop.
WordPress Netlify build hook plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Netlify build hook | |
* Plugin URI: https://dimitr.im | |
* Description: Call webhook when WordPress post is published | |
* Version: 1.0 | |
* Author: g00glen00b | |
* Author URI: https://dimitr.im | |
*/ | |
add_action('publish_future_post', 'nb_webhook_future_post', 10); | |
add_action('publish_post', 'nb_webhook_post', 10, 2); | |
add_action('publish_page', 'nb_webhook_post', 10, 2); | |
add_action('post_updated', 'nb_webhook_update', 10, 3); | |
function nb_webhook_future_post( $post_id ) { | |
nb_webhook_post($post_id, get_post($post_id)); | |
} | |
function nb_webhook_update($post_id, $post_after, $post_before) { | |
nb_webhook_post($post_id, $post_after); | |
} | |
function nb_webhook_post($post_id, $post) { | |
if ($post->post_status === 'publish') { | |
$url = curl_init('https://api.netlify.com/build_hooks/{my-build-hook}'); // TODO: Change this URL!! | |
curl_setopt($url, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($url, CURLOPT_RETURNTRANSFER, true); | |
curl_exec($url); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment