Scheduled Synchronization Using Cron

Preface

If you’re reading this page and you’re new to WP Fusion, then you’ve come to the wrong place.

WP Fusion can automatically sync data bidirectionally with your CRM without the use of cron jobs.

That’s achieved by setting up webhooks in your CRM, so your CRM notifies WP Fusion when a record has been modified, and the updated data is loaded into WordPress at that time.

Webhooks are way more efficient than loading data using a cron job or scheduled action.

Overview

There may be some scenarios where you need to re-sync or import data on a schedule, outside of webhooks.

For example maybe your CRM doesn’t support webhooks for the trigger you need, or your site is hosted on an internal network and can’t receive data from outside systems.

In that case you can schedule any one of WP Fusion’s batch operations using WordPress’ cron system.

To do so, you make a call to wp_fusion()->batch->batch_init( $method ); in your scheduled event callback, where $method is the name of the operation you’d like to perform.

You can see the internal batch operation names by inspecting the HTML radio values for the various batch operations on the Advanced tab in the WP Fusion settings.

The operation name will be the value parameter on the <input type="radio"> element.

Examples

Import every contact with a specific tag, daily

This snippet imports every new contact with tag ID 123, once daily.

function do_wpf_daily_import() {

	$args = array(
		'tag'    => 123, // The tag ID or list ID (with HubSpot) to import
		'role'   => 'subscriber',
		'notify' => false, // Set to true to send a welcome email
	);

	wp_fusion()->batch->batch_init( 'import_users', $args );

}

add_action( 'wpf_daily_import', 'do_wpf_daily_import' );

if ( ! wp_next_scheduled( 'wpf_daily_import' ) ) {
	wp_schedule_event( time(), 'daily', 'wpf_daily_import' );
}

Resync tags for all users every Friday

This snippet runs a Resync Tags operation every Friday.

function do_wpf_update_tags() {
	
	if ( date ('l') !== 'Friday' ) {
		return;
	}

	wp_fusion()->batch->batch_init( 'users_tags_sync' );

}

add_action( 'wpf_update_tags_weekly', 'do_wpf_update_tags' );

if ( ! wp_next_scheduled( 'wpf_update_tags_weekly' ) ) {
	wp_schedule_event( time(), 'daily', 'wpf_update_tags_weekly' );
}

Load metadata for all users, daily

This snippet runs a Pull User Meta operation daily at 5pm UTC.

function do_wpf_pull_meta() {

	wp_fusion()->batch->batch_init( 'pull_users_meta' );

}

add_action( 'wpf_pull_meta_daily', 'do_wpf_pull_meta' );

if ( ! wp_next_scheduled( 'wpf_update_tags_daily' ) ) {
	wp_schedule_event( strtotime( '5pm' ), 'daily', 'wpf_pull_meta_daily' );
}

Tips

Monitoring

We’d recommend using a cron management plugin like Advanced Cron Manager or WP Crontrol to ensure that the event has been scheduled, and to test the task.

Performance

The background processes works through records sequentially (there are no parallel threads), and is designed to use no more than 80% of your site’s available memory. Generally speaking, the background process should not be able to take your site offline.

However, it can make your site noticeably slower.

As a rule of thumb, assume that every user / record to be synced requires 5 seconds of processing time.

That means if you’re running a Resync Tags operation on 1,000 users daily, your site will be slowed down for about 80 minutes every day. That’s not the end of the world if you schedule the operation during the middle of the night.

However, if you have 10,000 users, the operation would be running for 13 hours of every day… which would likely cause a noticeable performance decrease. In that case it would be preferable to set the cron schedule so the task only runs once a week instead of once a day.

Was this helpful?