Action
uncanny-automator
automator_recipe_parts_duplicated
Fires after a recipe's parts are successfully duplicated to a new recipe, providing access to both the new and original recipe IDs.
add_action( 'automator_recipe_parts_duplicated', $callback, 10, 2 );
Description
Fires after all parts of a recipe have been successfully duplicated. Developers can use this hook to perform additional actions related to the duplicated recipe parts, such as updating metadata or triggering external services. This hook provides the ID of the new recipe and the original recipe.
Usage
add_action( 'automator_recipe_parts_duplicated', 'your_function_name', 10, 2 );
Parameters
-
$new_recipe_id(mixed) - This parameter contains the ID of the newly created recipe part after duplication.
-
$recipe_id(mixed) - The ID of the newly created recipe where the duplicated parts will be added.
Examples
<?php
/**
* Example of how to hook into the 'automator_recipe_parts_duplicated' action.
* This function will be executed after a recipe's parts have been duplicated.
* It logs the event to the WordPress debug log.
*
* @param int $new_recipe_id The ID of the newly created recipe.
* @param int $recipe_id The ID of the original recipe.
*/
function my_automator_recipe_parts_duplicated_handler( $new_recipe_id, $recipe_id ) {
// Ensure we have valid IDs before proceeding.
if ( ! absint( $new_recipe_id ) || ! absint( $recipe_id ) ) {
error_log( 'automator_recipe_parts_duplicated called with invalid recipe IDs.' );
return;
}
// Get recipe titles for more informative logging.
$new_recipe_title = get_the_title( $new_recipe_id );
$original_recipe_title = get_the_title( $recipe_id );
// Log the duplication event.
$message = sprintf(
'Automator: Recipe parts duplicated. New Recipe ID: %d ("%s"), Original Recipe ID: %d ("%s").',
$new_recipe_id,
$new_recipe_title,
$recipe_id,
$original_recipe_title
);
error_log( $message );
// You could also perform other actions here, such as:
// - Updating a custom meta field on the new recipe.
// - Triggering a notification to an administrator.
// - Performing additional data migrations if needed.
// Example: Update a meta field on the new recipe.
// update_post_meta( $new_recipe_id, '_duplicated_from_recipe_id', $recipe_id );
}
add_action( 'automator_recipe_parts_duplicated', 'my_automator_recipe_parts_duplicated_handler', 10, 2 );
?>
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/admin/class-copy-recipe-parts.php:212
public function copy_recipe_part( $recipe_id, $new_recipe_id, $type ) {
$recipe_parts = $this->get_recipe_parts_posts( $type, $recipe_id );
if ( empty( $recipe_parts ) ) {
return false;
}
foreach ( $recipe_parts as $recipe_part ) {
if ( $type !== $recipe_part->post_type ) {
continue;
}
$new_id = $this->copy( $recipe_part->ID, $new_recipe_id );
// Part duplicated
do_action( 'automator_recipe_part_duplicated', $new_id, $new_recipe_id, $recipe_part, $recipe_id, $type );
}
// Parts duplicated
do_action( 'automator_recipe_parts_duplicated', $new_recipe_id, $recipe_id );
return true;
}