Action Since 5.7 uncanny-automator

automator_recipe_created

Fires when a recipe is created. Fires when a new Automator recipe is successfully created, providing its post ID.

add_action( 'automator_recipe_created', $callback, 10, 1 );

Description

Fires immediately after a new Automator recipe is successfully created and saved. Developers can leverage this hook to perform custom actions, such as sending notifications, updating related data, or triggering further automation based on the newly created recipe's ID and its associated data.


Usage

add_action( 'automator_recipe_created', 'your_function_name', 10, 1 );

Parameters

$post_id (mixed)
- **$return** `mixed`

Examples

// Hook into the 'automator_recipe_created' action to perform custom actions when a new recipe is saved.
add_action( 'automator_recipe_created', 'my_custom_recipe_creation_handler', 10, 2 );

/**
 * Handles the creation of a new Automator recipe.
 *
 * This function logs the creation of a new recipe and optionally sends an email notification
 * to an administrator.
 *
 * @param int   $post_id The ID of the newly created recipe post.
 * @param array $return  The return data from the recipe creation process, including recipe details.
 */
function my_custom_recipe_creation_handler( $post_id, $return ) {
	// Ensure we have a valid recipe ID.
	if ( ! $post_id || ! is_numeric( $post_id ) ) {
		return;
	}

	// Log the recipe creation for debugging or auditing purposes.
	error_log( sprintf( 'New Automator recipe created with ID: %d', $post_id ) );

	// Get the recipe title for the notification.
	$recipe_title = get_the_title( $post_id );

	// Optionally, send an email notification to the site administrator.
	$admin_email = get_option( 'admin_email' );
	if ( $admin_email ) {
		$subject = sprintf( __( 'New Automator Recipe Created: %s', 'your-text-domain' ), $recipe_title );
		$message = sprintf(
			__( 'A new Automator recipe has been created on your site. nnRecipe Title: %s nRecipe ID: %d nnView recipe: %s', 'your-text-domain' ),
			$recipe_title,
			$post_id,
			admin_url( 'post.php?post=' . $post_id . '&action=edit' )
		);
		$headers = array( 'Content-Type: text/plain; charset=UTF-8' );

		// Uncomment the line below to actually send the email.
		// wp_mail( $admin_email, $subject, $message, $headers );

		error_log( sprintf( 'Notification email sent to %s for recipe ID %d.', $admin_email, $post_id ) );
	}

	// If $return contains specific data you want to process further, you can do it here.
	// For example, if you wanted to add a custom meta field based on the return data.
	if ( isset( $return['recipes_object']['ID'] ) && $return['recipes_object']['ID'] == $post_id ) {
		// Example: Update a custom meta field for the recipe.
		// update_post_meta( $post_id, '_automator_creation_processed', 'yes' );
	}
}

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:375

public function create( WP_REST_Request $request ) {
		$return['success'] = false;
		$return['data']    = $request;

		$recipe_post = array(
			'post_type'   => AUTOMATOR_POST_TYPE_RECIPE,
			'post_author' => get_current_user_id(),
		);

		if ( $request->has_param( 'recipeTitle' ) ) {
			$recipe_post['title'] = wp_strip_all_tags( $request->get_param( 'recipeTitle' ) );
		}

		$post_id = wp_insert_post( $recipe_post );

		if ( is_wp_error( $post_id ) ) {
			$return['message'] = sprintf( '%s:%s', esc_html__( 'The action failed to create the post. The response was', 'uncanny-automator' ), $post_id );

			return new WP_REST_Response( $return, 400 );
		}

		$return                   = array();
		$return['success']        = true;
		$return['post_ID']        = $post_id;
		$return['action']         = 'create';
		$return['recipes_object'] = Automator()->get_recipes_data( true, $post_id );

		/**
		 * Fires when a recipe is created.
		 *
		 * @since 5.7
		 */
		do_action( 'automator_recipe_created', $post_id, $return );

		return new WP_REST_Response( $return, 200 );
	}

Scroll to Top