Filter uncanny-automator

automator_recipe_export_object

Filters the recipe object before it is exported to allow modifications or custom data additions.

add_filter( 'automator_recipe_export_object', $callback, 10, 1 );

Description

Fires before a recipe object is exported. Developers can use this filter to modify or add data to the recipe export data. This hook is applied after the recipe's triggers, actions, loops, and closures have been fetched.


Usage

add_filter( 'automator_recipe_export_object', 'your_function_name', 10, 1 );

Parameters

$recipe (mixed)
This parameter contains the recipe object that is being prepared for export.

Return Value

The filtered value.


Examples

/**
 * Example: Add a custom field to the recipe export object.
 * This example demonstrates how to add a custom field with a specific value
 * to the recipe object before it's exported as JSON.
 *
 * @param array $recipe The recipe object being exported.
 * @return array The modified recipe object.
 */
add_filter( 'automator_recipe_export_object', function ( $recipe ) {

	// Check if the recipe is valid and not empty.
	if ( ! empty( $recipe ) && is_array( $recipe ) ) {
		// Add a custom field for demonstration purposes.
		// In a real scenario, you might fetch this data from another source
		// or base it on the recipe's content.
		$recipe['custom_export_data'] = 'This is custom data for recipe ID: ' . $recipe['ID'];
	}

	return $recipe;
}, 10, 1 );

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-export-recipe.php:258

public function fetch_recipe_as_json( $recipe_id ) {
		// Check if the post ID is valid
		$recipe_id = $this->validate_recipe_id( $recipe_id );

		if ( is_wp_error( $recipe_id ) ) {
			return $recipe_id;
		}

		$recipe = (object) array(
			'recipe'   => array(
				'post' => get_post( $recipe_id ),
				'meta' => $this->fetch_post_meta( $recipe_id ),
			),
			'triggers' => $this->fetch_recipe_parts( $recipe_id, AUTOMATOR_POST_TYPE_TRIGGER ),
			'actions'  => $this->fetch_recipe_parts( $recipe_id, AUTOMATOR_POST_TYPE_ACTION ),
			'loops'    => $this->fetch_recipe_parts( $recipe_id, AUTOMATOR_POST_TYPE_LOOP ),
			'closure'  => $this->fetch_recipe_parts( $recipe_id, AUTOMATOR_POST_TYPE_CLOSURE ),
		);

		$recipe = apply_filters( 'automator_recipe_export_object', $recipe );
		return wp_json_encode( $recipe, JSON_UNESCAPED_UNICODE );
	}

Scroll to Top