automator_wp_post_updates_prevent_trigger_on_rest_requests
Filters post updates to prevent triggering automations on REST API requests, based on the post ID.
add_filter( 'automator_wp_post_updates_prevent_trigger_on_rest_requests', $callback, 10, 1 );
Description
Filters whether WordPress post update triggers should fire during REST API requests. Set to `false` to prevent triggers from running when posts are updated via the REST API, avoiding unnecessary automation executions. Useful for performance optimization and preventing duplicate triggers.
Usage
add_filter( 'automator_wp_post_updates_prevent_trigger_on_rest_requests', 'your_function_name', 10, 1 );
Parameters
-
$post_id(mixed) - This parameter is used to indicate whether the trigger should be prevented from firing on REST requests.
Return Value
The filtered value.
Examples
<?php
/**
* Prevent Uncanny Automator triggers on REST API requests for post updates.
*
* This function is a callback for the 'automator_wp_post_updates_prevent_trigger_on_rest_requests'
* filter. It checks if the current request is a REST API request. If it is,
* it returns false to prevent the Automator trigger from firing.
*
* @param bool $prevent_trigger Whether to prevent the trigger. Defaults to true.
* @param int $post_id The ID of the post that was updated.
* @return bool Returns false to prevent the trigger, true otherwise.
*/
add_filter( 'automator_wp_post_updates_prevent_trigger_on_rest_requests', 'automator_prevent_post_update_trigger_on_rest', 10, 2 );
function automator_prevent_post_update_trigger_on_rest( $prevent_trigger, $post_id ) {
// Check if we are currently inside a REST API request.
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
// If this is a REST request and the filter explicitly says to prevent, return false.
// This ensures that if another plugin or custom code modifies the filter to allow
// REST requests, this function won't override it if $prevent_trigger is already false.
if ( $prevent_trigger ) {
return false;
}
}
// If it's not a REST request, or if the filter allowed it ($prevent_trigger is false),
// return the original value of $prevent_trigger to allow the trigger to proceed.
return $prevent_trigger;
}
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/wp/triggers/wp-anon-updates-post.php:87
src/integrations/wp/triggers/anon-wp-updates-post-in-taxonomy.php:194
uncanny-automator-pro/src/integrations/wp/triggers/wp-postinstatusupdated.php:131
uncanny-automator-pro/src/integrations/wp/triggers/wp-postintaxonomyupdated.php:155
protected function validate_trigger( ...$args ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return false;
}
list( $post_id, $wp_post_after, $wp_post_before ) = $args[0];
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
if ( apply_filters( 'automator_wp_post_updates_prevent_trigger_on_rest_requests', true, $post_id ) ) {
return;
}
}
// Prevent if publishing a post.
if ( 'publish' === $wp_post_after->post_status && 'publish' !== $wp_post_before->post_status ) {
return false;
}
$ignore_statuses = apply_filters(
'automator_wp_post_updates_ignore_statuses',
array(
'trash',
'draft',
'future',
),
$post_id,
$wp_post_after,
$wp_post_before
);
// Prevent if the status is excluded
if ( in_array( $wp_post_after->post_status, $ignore_statuses, true ) ) {
return false;
}
$include_non_public_posts = apply_filters( 'automator_wp_post_updates_include_non_public_posts', false, $post_id );
if ( false === $include_non_public_posts ) {
$__object = get_post_type_object( $wp_post_after->post_type );
if ( false === $__object->public ) {
return false;
}
}
if ( empty( $post_id ) ) {
return false;
}
return apply_filters( 'automator_wp_post_updates_post_updated', true, $post_id, $wp_post_after, $wp_post_before );
}