Action
Since 5.7
uncanny-automator
automator_recipe_terms_updated
Fires when a recipe terms are updated. Fires when terms associated with an Automator recipe are updated, providing recipe ID, taxonomy, and term IDs.
add_action( 'automator_recipe_terms_updated', $callback, 10, 2 );
Description
Fires after recipe terms are successfully updated via the REST API. Developers can use this hook to perform actions based on specific recipe terms being modified, such as invalidating caches or triggering related processes. The hook provides the recipe ID, taxonomy, updated term IDs, and the API response data.
Usage
add_action( 'automator_recipe_terms_updated', 'your_function_name', 10, 2 );
Parameters
-
$recipe_id(mixed) - - **$taxonomy** `mixed`
-
$term_ids(mixed) - - **$return** `mixed`
Examples
// Example callback function for the automator_recipe_terms_updated hook
function my_automator_handle_recipe_terms_update( $recipe_id, $taxonomy, $term_ids, $return_data ) {
// This example will log the update details to the WordPress debug log
// It's a common practice to log important events for debugging and auditing.
// Check if the recipe ID is valid (e.g., a positive integer)
if ( ! $recipe_id || ! is_numeric( $recipe_id ) || $recipe_id <= 0 ) {
error_log( 'automator_recipe_terms_updated: Invalid recipe ID received.' );
return; // Exit if recipe ID is invalid
}
// Ensure taxonomy is a string and term_ids is an array before proceeding
if ( ! is_string( $taxonomy ) || ! is_array( $term_ids ) ) {
error_log( 'automator_recipe_terms_updated: Invalid taxonomy or term_ids received for recipe ID ' . $recipe_id );
return; // Exit if taxonomy or term_ids are invalid
}
// Get the recipe title for more context in the log
$recipe_title = get_the_title( $recipe_id );
// Construct a descriptive message for the log
$log_message = sprintf(
'automator_recipe_terms_updated: Recipe "%s" (ID: %d) had its terms updated for taxonomy "%s". New term IDs: %s. Return data: %s',
$recipe_title,
$recipe_id,
$taxonomy,
implode( ',', $term_ids ), // Convert array of IDs to a comma-separated string
print_r( $return_data, true ) // Pretty print the return data array
);
// Use WP_DEBUG_LOG to write the message to the debug.log file
// Ensure WP_DEBUG and WP_DEBUG_LOG are enabled in your wp-config.php for this to work.
error_log( $log_message );
// You could also perform other actions here, such as:
// - Sending an email notification to an administrator
// - Triggering another process based on the term update
// - Updating custom meta fields for the recipe post
}
// Add the action hook with the callback function.
// The third parameter '4' indicates that this function accepts 4 arguments.
add_action( 'automator_recipe_terms_updated', 'my_automator_handle_recipe_terms_update', 10, 4 );
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/core/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:1315
public function set_recipe_terms( WP_REST_Request $request ) {
// Make sure we have a post ID and a post status
$params = $request->get_body_params();
if ( isset( $params['recipe_id'] ) && isset( $params['term_id'] ) ) {
$term_ids = array();
$update_count = false;
$recipe_id = absint( $params['recipe_id'] );
$taxonomy = (string) sanitize_text_field( $params['term_id'] );
if ( 'recipe_category' === $taxonomy && isset( $params['category_id'] ) && ! empty( $params['category_id'] ) ) {
$term_id = absint( $params['category_id'] );
$set_cat = 'true' === sanitize_text_field( $params['set_category'] ) ? true : false;
if ( true === $set_cat ) {
wp_add_object_terms( $recipe_id, $term_id, $taxonomy );
} elseif ( ! $set_cat ) {
wp_remove_object_terms( $recipe_id, $term_id, $taxonomy );
}
} elseif ( 'recipe_tag' === $taxonomy && isset( $params['tags']['commaSeparated'] ) && ! empty( $params['tags']['commaSeparated'] ) ) {
$tags_sanitized = sanitize_text_field( $params['tags']['commaSeparated'] );
$tags = explode( ',', $tags_sanitized );
wp_set_object_terms( $recipe_id, $tags, $taxonomy );
}
if ( $update_count ) {
$all_terms = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
)
);
if ( $all_terms ) {
$term_ids = array_column( $all_terms, 'term_id' );
wp_update_term_count_now( $term_ids, $taxonomy );
}
}
Automator()->cache->clear_automator_recipe_part_cache( $recipe_id );
$return['message'] = 'Updated!';
$return['success'] = true;
$return['action'] = 'set_recipe_terms';
/**
* Fires when a recipe terms are updated.
*
* @since 5.7
*/
do_action( 'automator_recipe_terms_updated', $recipe_id, $taxonomy, $term_ids, $return );
return new WP_REST_Response( $return, 200 );
}
$return['message'] = 'Failed to update';
$return['success'] = false;
$return['action'] = 'show_error';
return new WP_REST_Response( $return, 200 );
}