Filter Since 3.0 uncanny-automator

automator_add_recipe_child

Filters whether a post type can be added as a child recipe, triggered when adding a new recipe.

add_filter( 'automator_add_recipe_child', $callback, 10, 4 );

Description

Fires before a child recipe post is created. Developers can filter the return value to prevent post creation or modify the arguments. Expects a boolean `true` to allow creation; any other WP_REST_Response object will halt the process. The parameters `$post_type`, `$action`, and `$recipe` provide context for the child recipe.


Usage

add_filter( 'automator_add_recipe_child', 'your_function_name', 10, 4 );

Parameters

$create_post (bool)
Whether to create the post. Default true.
$post_type (string)
The post type being created (uo-trigger, uo-action, uo-closure).
$action (string)
The action being performed (create_trigger, create_action, create_closure).
$recipe (WP_Post)
The parent recipe post object.

Return Value

The filtered value.


Examples

add_filter(
	'automator_add_recipe_child',
	function ( $should_create, $post_type, $action, $recipe ) {
		// Example: Prevent creating a specific post type for a recipe if it has a certain tag.
		// This is a simplified example; in a real scenario, you'd likely interact with
		// WordPress functions to check post meta, terms, etc.

		// Check if the recipe is of a type that we want to conditionally modify
		if ( 'your_recipe_post_type_slug' === $post_type && is_array( $recipe ) && isset( $recipe['ID'] ) ) {

			// Hypothetical check: if the recipe post has a meta key 'do_not_create_child' set to 'yes'
			// In a real plugin, you'd use get_post_meta( $recipe['ID'], 'do_not_create_child', true )
			$do_not_create_child = get_post_meta( $recipe['ID'], 'do_not_create_child', true );

			if ( 'yes' === $do_not_create_child ) {
				// Return a WP_REST_Response to signal an error and stop the process
				return new WP_REST_Response(
					array(
						'message' => esc_html__( 'Child post creation is disabled for this recipe.', 'uncanny-automator' ),
					),
					403 // Forbidden status code
				);
			}
		}

		// If no conditions are met to prevent creation, return the original value (true)
		// allowing the recipe child to be created.
		return $should_create;
	},
	10, // Priority
	4  // Accepted arguments: $should_create, $post_type, $action, $recipe
);

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:458
src/api/transports/restful/recipe/items/traits/filters/trait-filter-before-item-created.php:40

protected function apply_creation_filter( string $post_type, string $action ) {

		/**
		 * Filters whether to allow creation of a recipe child item.
		 *
		 * @since 3.0
		 *
		 * @param bool    $create_post Whether to create the post. Default true.
		 * @param string  $post_type   The post type being created (uo-trigger, uo-action, uo-closure).
		 * @param string  $action      The action being performed (create_trigger, create_action, create_closure).
		 * @param WP_Post $recipe      The parent recipe post object.
		 */
		$result = apply_filters(
			'automator_add_recipe_child',
			true,
			$post_type,
			$action,
			Database::get_recipe_store()->get_wp_post( $this->get_recipe_id() )
		);

		if ( true === $result ) {
			return true;
		}

		return $this->normalize_filter_response( $result );
	}

Scroll to Top