Action
uncanny-automator
automator_cache_recipe_post_deleted
Fires when a recipe post is deleted, allowing for custom actions to be taken in response.
add_action( 'automator_cache_recipe_post_deleted', $callback, 10, 1 );
Description
Fires after a WordPress post related to a recipe has been deleted. Developers can use this hook to perform custom actions, such as cleaning up related transient caches or initiating further cleanup processes when a recipe's associated post is removed from the system.
Usage
add_action( 'automator_cache_recipe_post_deleted', 'your_function_name', 10, 1 );
Parameters
-
$args(mixed) - This parameter contains the ID of the deleted WordPress post.
Examples
add_action( 'automator_cache_recipe_post_deleted', 'my_automator_clear_related_post_caches', 10, 1 );
/**
* Clears custom caches when a recipe post is deleted.
*
* This function hooks into the 'automator_cache_recipe_post_deleted' action.
* When a post related to an Automator recipe is deleted, this function
* will perform additional cache clearing relevant to that specific post.
*
* @param int $post_id The ID of the deleted post.
*/
function my_automator_clear_related_post_caches( $post_id ) {
// Ensure we have a valid post ID.
if ( ! $post_id || ! is_numeric( $post_id ) ) {
return;
}
// Example: If you have specific caches tied to individual recipe posts,
// you might want to clear them here based on the post ID.
// For instance, if you cache a list of meta values for each recipe.
$recipe_meta_cache_key = 'automator_recipe_meta_' . $post_id;
delete_transient( $recipe_meta_cache_key ); // Using delete_transient as a realistic example of cache clearing.
// You could also potentially clear caches related to the user who created the recipe,
// or other related data that might be cached per recipe.
// $recipe_author_id = get_post_field( 'post_author', $post_id );
// if ( $recipe_author_id ) {
// delete_transient( 'automator_user_recipes_' . $recipe_author_id );
// }
}
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/lib/helpers/class-automator-cache-handler.php:376
public function recipe_post_deleted( ...$args ) {
if ( empty( $args ) ) {
return;
}
$post_id = isset( $args[0] ) ? absint( $args[0] ) : 0;
if ( 0 === $post_id ) {
return;
}
$post = isset( $args[1] ) ? $args[1] : get_post( $post_id );
if ( ! $post instanceof WP_Post ) {
return;
}
// prepare transient key.
$transient_key = apply_filters( 'automator_transient_name', 'automator_transient', array() );
// suffix post type is needed.
$transient_key .= md5( wp_json_encode( $post->post_type ) );
$this->remove( $transient_key );
// Clear recipes data cache
$this->remove( $this->recipes_data );
$this->remove( 'automator_actionified_triggers' );
do_action( 'automator_cache_recipe_post_deleted', $args );
}