Action
uncanny-automator
automator_copy_recipe_complete
Fires after a recipe has been successfully copied.
add_action( 'automator_copy_recipe_complete', $callback, 10, 2 );
Description
Fires after a recipe has been successfully copied. Developers can use this hook to perform actions on the newly created recipe or the original recipe after the copying process is complete. Both the new and original recipe IDs are passed as arguments.
Usage
add_action( 'automator_copy_recipe_complete', 'your_function_name', 10, 2 );
Parameters
-
$new_recipe_id(mixed) - The ID of the newly created copied recipe.
-
$recipe_id(mixed) - The ID of the newly created recipe after the copy operation is complete.
Examples
<?php
/**
* Example callback function for the 'automator_copy_recipe_complete' action hook.
* This function logs the details of a recipe copy operation.
*
* @param int $new_recipe_id The ID of the newly created recipe.
* @param int $recipe_id The ID of the original recipe that was copied.
*/
function my_automator_log_recipe_copy_complete( $new_recipe_id, $recipe_id ) {
// Ensure the IDs are valid integers before proceeding.
if ( ! is_numeric( $new_recipe_id ) || ! is_numeric( $recipe_id ) ) {
error_log( 'Uncanny Automator: Invalid recipe IDs passed to automator_copy_recipe_complete.' );
return;
}
$new_recipe_title = get_the_title( $new_recipe_id );
$original_recipe_title = get_the_title( $recipe_id );
// Log the successful copy operation.
$log_message = sprintf(
'Uncanny Automator: Recipe copied successfully. Original Recipe ID: %1$d ("%2$s"), New Recipe ID: %3$d ("%4$s").',
absint( $recipe_id ),
esc_html( $original_recipe_title ),
absint( $new_recipe_id ),
esc_html( $new_recipe_title )
);
// You could also perform other actions here, such as updating a meta field,
// sending a notification, or triggering another process.
// For example, updating a custom meta field on the new recipe:
// update_post_meta( $new_recipe_id, '_copied_from_recipe', $recipe_id );
error_log( $log_message );
}
// Hook into the 'automator_copy_recipe_complete' action.
// The '10' is the priority, and '2' indicates that the callback function accepts two arguments.
add_action( 'automator_copy_recipe_complete', 'my_automator_log_recipe_copy_complete', 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:123
public function copy_recipe_parts() {
if ( ! automator_filter_has_var( 'action' ) ) {
return;
}
if ( 'copy_recipe_parts' !== automator_filter_input( 'action' ) ) {
return;
}
if ( ! automator_filter_has_var( 'post' ) ) {
return;
}
if ( ! automator_filter_has_var( '_wpnonce' ) ) {
return;
}
if ( ! wp_verify_nonce( automator_filter_input( '_wpnonce' ), 'Aut0Mat0R' ) ) {
return;
}
$recipe_id = absint( automator_filter_input( 'post' ) );
if ( ! current_user_can( 'edit_post', $recipe_id ) ) {
return;
}
if ( AUTOMATOR_POST_TYPE_RECIPE !== get_post_type( $recipe_id ) ) {
wp_die( esc_attr( sprintf( '%s %s', esc_html__( 'Copy creation failed, could not find original recipe:', 'uncanny-automator' ), htmlspecialchars( $recipe_id ) ) ) );
}
// Copy the post and insert it
$new_recipe_id = $this->copy_this_recipe( $recipe_id );
do_action( 'automator_copy_recipe_complete', $new_recipe_id, $recipe_id );
if ( automator_filter_has_var( 'return_to_recipe' ) ) {
wp_safe_redirect( admin_url( 'post.php?post=' . $new_recipe_id . '&action=edit' ) );
} else {
if ( false === $new_recipe_id ) {
wp_safe_redirect( admin_url( 'post.php?post=' . $recipe_id . '&action=edit' ) );
exit;
}
wp_safe_redirect( admin_url( 'edit.php?post_type=' . get_post_type( $recipe_id ) ) );
}
exit;
}