How to Crosspost Programmatically

Let’s assume you’re creating your plugins for WordPress or maybe working with some third-party plugin, and you want it to work together in some way with my crossposting plugins.

For example, when you send a form from the front end of your website, you would like to crosspost a post to another site. How to do that?

Good news – there are two ways how you can do that and I’m going to uncover it below.

Method 1. save_post hook

The easiest way is to run a standard WordPress hook save_post inside your plugin.

If you decide to use this method, please keep in mind the following:

  • It works only when “Auto-Mode” is activated,
  • The user must be authenticated and have the capability to edit the target post.

Here is an example of how you can use it:

// we're kind of emulating editing of this post in the WordPress editor
$_POST[ 'action' ] = 'editpost';
// you need to pass $post_id and $post variables to the hook
do_action( 'save_post', $post_id, $post, true );

As you can see, we can not just run the save_post hook, we also need to provide the $_POST[ 'action' ] with the editpost value; otherwise, my plugins would trigger crossposting in many WordPress functions where it is not needed at all.

The only thing – in the WordPress admin there are also checkboxes where we need to select specific sites. That’s why I asked you to turn on the “Auto Mode.”

In Simple WP Crossposting you can do it in Settings > Crosspost:

The Auto Mode option in Simple WP Crossposting plugin

In Simple Multisite Crossposting you need to go to a specific site settings in Sites > All sites:

The Auto Mode option in Simple Multisite Crossposting plugin

Please also keep in mind that my plugins check users’ capabilities. For example, if you try to run this code for a user, let’s say a subscriber, who can not edit posts, nothing will happen.

Example. Triggering Crossposting from Gravity Forms

For example, let’s say that you’re using Gravity Forms on your site, which updates a specific custom field of a specific post. Sure, after updating this custom field, you need to reflect the changes on a cross-posted copy of this post.

First of all, I’d like to remind you that in order to trigger crossposting after submitting a form, you need to use gform_after_submission action hook, which is well documented here.

Here is the code example you can use:

add_action( 'gform_after_submission', function( $entry, $form ) {

	// we need to run this code only for a specific form
	if( 123 != $form[ 'id' ] ) {
		return;
	}
	// we can check the title as well
	// if( 'Testing Edit' !== $form[ 'title' ] ) {
	// 	return;
	// }

	// get variables from the form fields using the rgar() function
	$post_id = rgar( $entry, '1' );
	$status = rgar( $entry, '4' );

	// check if this post does actually exist
	if( ! $post = get_post( $post_id ) ) {
		return;
	}

	// update meta data of the original post
	update_post_meta( $post_id, 'order-details-status', $status );
	
	// syncing the changes
	$_POST[ 'action' ] = 'editpost';
	do_action( 'save_post', $post_id, $post, true );

}, 99, 2 );

Method 2. Using Crossposting PHP Classes

This method is more advanced compared to the previous one. However, it has some serious advantages:

  • You can use it wherever you want on your website and for unauthorized users.
  • You can decide which sites to crosspost a specific post to.

If you’re using a multisite version of the plugin:

// regular posts, pages and custom post types
$crosspost = new Rudr_Simple_Multisite_Crosspost();
$crosspost->crosspost( $post, $blog_ids );
$postWP_Post
A post object.
$blog_idsint[]
An array with blog IDs in your WordPress multisite network you’re going to crosspost to.
// WooCommerce
$woocommerce_crosspost = new Rudr_Simple_Multisite_Woo_Crosspost();
$woocommerce_crosspost->crosspost_product( $product_id, $store_ids );
$product_idint
A product ID.
$store_idsint[]
An array with store IDs within your WooCommerce multisite network you’re going to crosspost to.

If you’re using a non-multisite version of the plugin:

// regular WordPress posts, pages and custom post types
$crosspost = new Rudr_Simple_WP_Crosspost();
$crosspost->crosspost( $post, $blogs );
$postWP_Post
A post object.
$blogsarray[]
Here you need to pass an array with target sites, you can use $crosspost->get_blogs() method in order to get them and then remove some specific sites from there.
// WooCommerce
$woocommerce_crosspost = new Rudr_Simple_Woo_Crosspost();
$woocommerce_crosspost->crosspost_product( $product_id, $stores );
$product_idint
A product ID.
$storesarray[]
An array with target stores. You can use $crosspost->get_blogs() here as well.

Need more help?