Filter uncanny-automator

automator_schedule_a_post_time_for_elem

Filters the scheduled post time for Elementor posts, allowing modification before the post is saved.

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

Description

Filters the scheduled time for the `elem_wp_after_insert_post` event, which fires after a post is published via Elementor. Developers can adjust this delay to ensure all post data, including terms and taxonomies, is fully saved before the event executes. The default delay is 10 seconds.


Usage

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

Parameters

$post_id (mixed)
This parameter represents the timestamp when the post is scheduled to be published, with a 10-second buffer added to the current time.
$post (mixed)
This parameter contains the ID of the post that is being scheduled for publication.
$update (mixed)
This parameter contains the full post object.

Return Value

The filtered value.


Examples

/**
 * Adjust the scheduled time for Elementor post publications if the post is a draft.
 *
 * This filter allows for modifying the scheduled time for the 'elem_wp_after_insert_post'
 * cron event. If the post is in draft status, we might want to delay its processing
 * further to ensure all meta data and terms are correctly saved before the scheduled event runs.
 *
 * @param int $scheduled_time The original scheduled Unix timestamp.
 * @param int $post_id        The ID of the post being processed.
 * @param WP_Post $post       The post object.
 * @param bool $update        Whether this is an update to an existing post.
 * @return int The adjusted scheduled Unix timestamp.
 */
add_filter( 'automator_schedule_a_post_time_for_elem', function( $scheduled_time, $post_id, $post, $update ) {

	// Check if the post is a draft. If so, add an extra 5 seconds to the schedule.
	// This is a hypothetical scenario where additional processing might be needed for drafts.
	if ( 'draft' === $post->post_status ) {
		$scheduled_time = $scheduled_time + 5;
	}

	// Return the (potentially modified) scheduled time.
	return $scheduled_time;

}, 10, 4 ); // 10 is the priority, 4 is the number of arguments accepted by the callback function.

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/elementor/triggers/elem-post-published.php:160

public function schedule_a_post( $post_id, $post, $update, $post_before ) {
		// only run when posts
		// are published first time
		if ( ! Automator()->utilities->is_wp_post_being_published( $post, $post_before ) ) {
			return;
		}

		// if post is not published with Elementor.
		$created_by_elem = get_post_meta( $post_id, '_elementor_edit_mode', true );
		if ( empty( $created_by_elem ) ) {
			// Immediately run validate_conditions
			return;
		}

		$cron_enabled = apply_filters( 'automator_elem_user_creates_post_cron_enabled', true, $post_id, $post, $update, $this );

		// Allow people to disable cron processing.
		if ( false === $cron_enabled ) {
			// Immediately run post_publised if cron not enabled.
			$this->post_published( $post_id );

			return;
		}

		if ( wp_next_scheduled( 'elem_wp_after_insert_post', array( $post_id ) ) ) {
			return;
		}

		// Scheduling for 10 sec so that all tax/terms are stored
		return wp_schedule_single_event(
			apply_filters( 'automator_schedule_a_post_time_for_elem', time() + 10, $post_id, $post, $update ),
			'elem_wp_after_insert_post',
			array(
				$post_id,
			)
		);
	}


Scroll to Top