Last active
February 15, 2021 11:24
-
-
Save freddiemixell/89bd5c4f5732e66ebb5adafbba3120f9 to your computer and use it in GitHub Desktop.
WordPress Netlify Build Hook
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 | |
| // Somewhere off in your wp-config.php make sure you replace 123456 with your api key. | |
| define( 'NETLIFY_API_KEY', '123456' ); | |
| // Somewhere off in your theme or even a plugin. | |
| /** | |
| * Netlify Webhook Build Function | |
| * | |
| * This function will trigger a rebuild on post update | |
| * | |
| * @param int $post_id ID of post which may or may not deploy Netlify. | |
| */ | |
| add_action( 'save_post', 'fm_netlify_webhook_build' ); | |
| if ( ! function_exists( 'fm_netlify_webhook_build' ) ) { | |
| function fm_netlify_webhook_build( $post_id ) { | |
| // Exit out on post revisions. | |
| if ( wp_is_post_revision( $post_id ) ) { | |
| return; | |
| } | |
| // If you haven't make sure you define your Netlify api key! | |
| if ( ! defined( 'NETLIFY_API_KEY' ) { | |
| write_log( 'Your Netlify API Key is not defined homie!' ); | |
| return false; | |
| } | |
| $post_status = get_post_status( $post_id ); | |
| if ( 'publish' === $post_status || 'draft' === $post_status ) { | |
| $api_key = NETLIFY_API_KEY; | |
| $netlify_hook = "https://api.netlify.com/build_hooks/{$api_key}"; | |
| $response = wp_safe_remote_post( $netlify_hook, array( | |
| 'headers' => array( 'Content-Type: application/x-www-form-urlencoded' ), | |
| 'body' => '{}', | |
| ) | |
| ); | |
| if ( is_wp_error( $response ) ) { | |
| $error_message = $response->get_error_message(); | |
| /** | |
| * Log Errors | |
| * You'll need to have the following defined in your wp-config.php: | |
| * define( 'WP_DEBUG', true ); | |
| * define( 'WP_DEBUG_DISPLAY', false ); | |
| * define( 'WP_DEBUG_LOG', true ); | |
| */ | |
| write_log( $error_message ); | |
| } | |
| } | |
| } | |
| } | |
| // Thanks Elegant Themes! | |
| // @see https://www.elegantthemes.com/blog/tips-tricks/using-the-wordpress-debug-log | |
| if ( ! function_exists('write_log')) { | |
| function write_log ( $log ) { | |
| if ( is_array( $log ) || is_object( $log ) ) { | |
| error_log( print_r( $log, true ) ); | |
| } else { | |
| error_log( $log ); | |
| } | |
| } | |
| } |
Author
Thanks for checking it out I'm sorry I'm just seeing this now @jamie-endeavour! I see what you're saying, there should be another deploy because the page is not longer live. I'm going to update the gist thanks for the help homie.
No worries! 👍
Author
Latest version adds error handling if you don't have an api key or if you receive an error from Netlify's api.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! I had a function similar to this but I was having some trouble with deployments because they were being triggered all over the place, turns out I was missing the
get_post_statuscheck so this is definitely a nice touch. However, after testing with your condition to check if the post status ispublishI found that my deployments weren't working when changing status from 'published' to 'draft'.I updated the post status test to:
And this sorted it out nicely for me.
Again, thanks very much for the share!
Reference: List of possible posts statuses