Filter
uncanny-automator
automator_recipe_export_parts
Filters recipe export parts before they are saved, allowing modification of the data.
add_filter( 'automator_recipe_export_parts', $callback, 10, 4 );
Description
Filters the parts of a recipe being exported. Developers can use this hook to modify or add to the data associated with a recipe before it's exported, allowing for custom export logic or data manipulation. The hook fires after all recipe parts have been fetched.
Usage
add_filter( 'automator_recipe_export_parts', 'your_function_name', 10, 4 );
Parameters
-
$parts(mixed) - This parameter contains an array of recipe parts that will be exported.
-
$recipe_parts(mixed) - This parameter contains an array of all recipe parts, which can be filtered or modified.
-
$post_id(mixed) - This parameter contains the recipe parts data fetched from the database for the specified recipe post.
-
$post_type(mixed) - This parameter contains the ID of the post that represents the recipe being exported.
Return Value
The filtered value.
Examples
/**
* Modify the exported recipe parts to include additional metadata for a specific recipe type.
*
* This example demonstrates how to hook into 'automator_recipe_export_parts'
* to add custom information to the exported recipe data.
*
* @param array $parts The array of recipe parts to be exported.
* @param array $recipe_parts The original recipe parts data.
* @param int $post_id The ID of the recipe post.
* @param string $post_type The post type of the recipe.
*
* @return array Modified array of recipe parts.
*/
function my_automator_modify_recipe_export_parts( $parts, $recipe_parts, $post_id, $post_type ) {
// Check if this is a specific recipe type we want to modify.
// Replace 'your_custom_recipe_type' with the actual post type slug if needed.
if ( 'your_custom_recipe_type' === $post_type ) {
// For demonstration, let's add a custom key to each part.
// In a real-world scenario, you might fetch related data from the database
// or perform other manipulations based on the $recipe_parts.
foreach ( $parts as $key => $part ) {
if ( is_object( $part ) && isset( $part->ID ) ) {
$parts[ $key ]->custom_export_info = 'This part is for a custom recipe type.';
// You could also potentially modify existing data or add new loops/elements.
// For example:
// $parts[ $key ]->my_new_data = get_post_meta( $part->ID, '_my_custom_meta', true );
}
}
}
// Always return the modified (or original) parts.
return $parts;
}
add_filter( 'automator_recipe_export_parts', 'my_automator_modify_recipe_export_parts', 10, 4 );
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:317
public function fetch_recipe_parts( $post_id, $post_type ) {
if ( is_null( $this->copy_recipe_parts ) ) {
$this->copy_recipe_parts = Automator_Load::get_core_class_instance( 'Copy_Recipe_Parts' );
}
$recipe_parts = $this->copy_recipe_parts->get_recipe_parts_posts( $post_type, $post_id );
if ( empty( $recipe_parts ) ) {
return false;
}
$parts = array();
foreach ( $recipe_parts as $r => $recipe_part ) {
if ( $post_type !== $recipe_part->post_type ) {
continue;
}
$parts[ $r ] = (object) array(
'post' => $recipe_part,
'meta' => $this->fetch_post_meta( $recipe_part->ID ),
);
if ( AUTOMATOR_POST_TYPE_LOOP === $post_type ) {
$parts[ $r ]->loops = array(
'filters' => $this->fetch_recipe_parts( $recipe_part->ID, AUTOMATOR_POST_TYPE_LOOP_FILTER ),
'actions' => $this->fetch_recipe_parts( $recipe_part->ID, AUTOMATOR_POST_TYPE_ACTION ),
);
}
}
return ( empty( (array) $parts ) ) ? false : apply_filters( 'automator_recipe_export_parts', $parts, $recipe_parts, $post_id, $post_type );
}