Filter uncanny-automator

automator_create_post_return_wp_error

Filters the WP_Error object returned when creating a post within the Automator plugin.

add_filter( 'automator_create_post_return_wp_error', $callback, 10, 3 );

Description

This filter hook allows developers to intercept and modify the WP_Error object returned by `wp_insert_post` when creating a post within an automation. By default, it returns `true` to ensure `wp_insert_post` returns a `WP_Error` object on failure. Developers can return `false` to prevent this, or modify the `WP_Error` object itself.


Usage

add_filter( 'automator_create_post_return_wp_error', 'your_function_name', 10, 3 );

Parameters

$post_args (mixed)
This parameter indicates whether a `WP_Error` object should be returned if the post creation fails.
$recipe_id (mixed)
This parameter contains an array of arguments used to create the WordPress post.
$action_data (mixed)
This parameter contains the ID of the recipe that triggered the post creation.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the behavior of wp_insert_post when creating a post via Automator.
 *
 * This filter allows us to intercept the return value of wp_insert_post
 * and decide whether to return a WP_Error object or the post ID.
 * By default, it returns true, meaning wp_insert_post will return the post ID
 * or a WP_Error object. If we return false, wp_insert_post will only return
 * the post ID on success and false on failure (though this might change the
 * intended behavior of the original plugin).
 *
 * @param bool   $return_wp_error Whether to return a WP_Error object on failure.
 * @param array  $post_args       The arguments used to create the post.
 * @param int    $recipe_id       The ID of the Automator recipe.
 * @param array  $action_data     The data associated with the Automator action.
 *
 * @return bool True to return WP_Error on failure, false otherwise.
 */
add_filter( 'automator_create_post_return_wp_error', function ( $return_wp_error, $post_args, $recipe_id, $action_data ) {

	// Let's say we only want to return a WP_Error object if the post title is empty
	// and the post type is 'page'. Otherwise, we'll let it behave as default.
	if ( isset( $post_args['post_title'] ) && empty( $post_args['post_title'] ) && isset( $post_args['post_type'] ) && 'page' === $post_args['post_type'] ) {
		// We could potentially add a custom error here, but for demonstration,
		// we'll just ensure it returns a WP_Error by setting this to true.
		// The actual WP_Error will be generated by wp_insert_post itself.
		return true;
	}

	// For all other cases, maintain the default behavior.
	return $return_wp_error;

}, 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/integrations/wp/actions/wp-createpost.php:460

if ( ! empty( $user ) ) {
				$post_args['post_author'] = absint( $user->ID );
				break;
			}
		}

		$return_wp_error  = apply_filters( 'automator_create_post_return_wp_error', true, $post_args, $recipe_id, $action_data );
		$fire_after_hooks = apply_filters( 'automator_create_post_fire_after_hooks', true, $post_args, $recipe_id, $action_data );

		$post_id = wp_insert_post( $post_args, $return_wp_error, $fire_after_hooks );

		if ( is_wp_error( $post_id ) ) {
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $post_id->get_error_message() );


Scroll to Top