Skip to content

Instantly share code, notes, and snippets.

@elron
Created October 19, 2024 14:20
Show Gist options
  • Save elron/bd1726996731656c0056554f8c098dc4 to your computer and use it in GitHub Desktop.
Save elron/bd1726996731656c0056554f8c098dc4 to your computer and use it in GitHub Desktop.
Rebuild Netlify on WordPress Taxonomy Update
<?php
/**
* Plugin Name: Rebuild Netlify on Poem or Tag Update
* Description: Triggers Netlify build hook when a "poem" post or "tag" taxonomy is updated.
* Version: 1.0
* Author: Elron Bucai & ChatGPT
* Author URI: https://www.elrons.co.il/
*/
// Function to trigger the Netlify build hook
function trigger_netlify_build_hook()
{
// Replace the build hook URL below with your actual Netlify build hook URL.
$netlify_build_hook_url = 'https://api.netlify.com/build_hooks/XXXXXXXXXXXXXXXXXX';
// Trigger the Netlify build hook using cURL.
$curl = curl_init($netlify_build_hook_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{}');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// Log the response if needed (optional).
error_log('Netlify Build Hook Response: ' . print_r($response, true));
}
// Hook into WordPress and trigger the Netlify build hook when a "poem" post is updated or deleted.
function rebuild_netlify_on_poem_update($post_ID)
{
// Check if the post is of "poem" post type.
if (get_post_type($post_ID) === 'poem') {
trigger_netlify_build_hook();
}
if (get_post_type($post_ID) === 'page') {
trigger_netlify_build_hook();
}
}
add_action('post_updated', 'rebuild_netlify_on_poem_update');
add_action('before_delete_post', 'rebuild_netlify_on_poem_update');
// Hook into WordPress and trigger the Netlify build hook when a new "poem" post is published.
function rebuild_netlify_on_new_poem($post_ID)
{
// Check if the post is of "poem" post type and it's newly published.
if (get_post_type($post_ID) === 'poem' && get_post_status($post_ID) === 'publish') {
trigger_netlify_build_hook();
}
}
add_action('publish_poem', 'rebuild_netlify_on_new_poem');
// Hook into WordPress and trigger the Netlify build hook when any term (including tags) is created, edited, or deleted.
function rebuild_netlify_on_term_update($term_id, $tt_id, $taxonomy, $deleted_term)
{
// Check if the taxonomy is "post_tag" (general tags).
if ($taxonomy === 'post_tag') {
trigger_netlify_build_hook();
}
}
add_action('create_term', 'rebuild_netlify_on_term_update', 10, 4);
add_action('edited_term', 'rebuild_netlify_on_term_update', 10, 4);
add_action('delete_term', 'rebuild_netlify_on_term_update', 10, 4);
add_action('acf/options_page/save', 'my_acf_save_options_page', 10, 2);
function my_acf_save_options_page($post_id, $menu_slug)
{
if ('site-options' === $menu_slug) {
trigger_netlify_build_hook();
}
if ('synonyms' === $menu_slug) {
trigger_netlify_build_hook();
}
}
add_action('acp/editing/saved', 'check_tag_update', 10, 3);
function check_tag_update(AC\Column $column, $id, $value)
{
// error_log(print_r([
// 'column' => $column,
// 'id' => $id,
// 'value' => $value,
// 'get_type' => $column->get_type(),
// '$column->get_taxonomy()' => $column->get_taxonomy(),
// ], true));
// if tag name or custom field was updated
if ($column->get_taxonomy() === 'post_tag') {
trigger_netlify_build_hook();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment