Filter uncanny-automator

automator_recipe_export_json

Filters the JSON data before a recipe is exported, allowing modification of its content.

add_filter( 'automator_recipe_export_json', $callback, 10, 2 );

Description

Filters the JSON data of a recipe before it's exported. Developers can modify the recipe's JSON representation, allowing for custom export formats or data manipulation. This hook fires during the recipe export process.


Usage

add_filter( 'automator_recipe_export_json', 'your_function_name', 10, 2 );

Parameters

$json (mixed)
This parameter holds the JSON representation of the recipe data that will be exported.
$recipe_id (mixed)
This parameter contains the JSON data representing the recipe that is being exported.

Return Value

The filtered value.


Examples

/**
 * Example of how to hook into the 'automator_recipe_export_json' filter to modify the exported JSON.
 * This example demonstrates adding a custom meta field to the exported JSON data.
 *
 * @param mixed $json The original JSON data of the recipe.
 * @param int   $recipe_id The ID of the recipe being exported.
 *
 * @return mixed The modified JSON data.
 */
add_filter(
	'automator_recipe_export_json',
	function ( $json, $recipe_id ) {
		// Ensure $json is an array before attempting to modify it.
		// If $json is already a WP_Error object, it will be handled later.
		if ( ! is_array( $json ) ) {
			return $json;
		}

		// Fetch additional custom data for this recipe.
		// In a real scenario, this would involve querying the database
		// or using WordPress functions to get specific meta.
		$custom_export_data = get_post_meta( $recipe_id, '_my_custom_export_field', true );

		// Add the custom data to the JSON payload.
		// We can append it, or merge it depending on the desired structure.
		// Here, we'll add it as a new key.
		$json['my_custom_export_data'] = $custom_export_data;

		// Return the modified JSON data.
		return $json;
	},
	10, // Priority: 10 is the default, meaning it runs after most other filters.
	2  // Accepted args: This filter expects two arguments ($json, $recipe_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/admin/class-export-recipe.php:64
src/core/admin/class-export-recipe.php:104
src/core/admin/class-export-recipe.php:120

public function export_recipe_json() {

		if ( ! automator_filter_has_var( 'action' ) ) {
			return;
		}

		if ( 'export_recipe' !== automator_filter_input( 'action' ) ) {
			return;
		}

		if ( ! automator_filter_has_var( '_wpnonce' ) ) {
			$this->die_with_error( _x( 'Security issue, invalid nonce. Please refresh the page and try again.', 'Export Recipe', 'uncanny-automator' ) );
		}

		if ( ! wp_verify_nonce( automator_filter_input( '_wpnonce' ), 'Aut0Mat0R' ) ) {
			$this->die_with_error( _x( 'Security issue, invalid nonce. Please refresh the page and try again.', 'Export Recipe', 'uncanny-automator' ) );
		}

		$recipe_id = absint( automator_filter_input( 'post' ) );

		if ( ! current_user_can( 'edit_post', $recipe_id ) ) {
			$this->die_with_error( _x( 'You do not have permission to export this recipe.', 'Export Recipe', 'uncanny-automator' ) );
		}

		$json = $this->fetch_recipe_as_json( $recipe_id );
		$json = apply_filters( 'automator_recipe_export_json', $json, $recipe_id );

		if ( is_wp_error( $json ) ) {
			$this->die_with_error( $json->get_error_message() );
		}

		$filename = $this->generate_filename( $recipe_id );

		$this->handle_download( $json, $filename );
	}

Scroll to Top