Multisite Crossposting Hook Reference

If there is not enough functionality in the plugin settings for you, then you can configure everything with actions and filters.

If you are going to use a code snippet with a hook, the best way you can do it is by creating a custom plugin. In order to do so, please create an empty PHP file in your wp-content/plugins directory, for example my-custom-super-plg.php. Add these lines:

<?php
/* 
 * Plugin name: My plugin 
 * Network: true
 */

/// here you can insert the snippets

And then “Network activate” it.

rudr_smc_allowed_blog_ids

$allowed_blog_ids = apply_filters( 'rudr_smc_allowed_blog_ids', $allowed_blog_ids );
$allowed_blog_idsint[]
You can provide your own array of blog IDs you would like to allow crossposting to.

Example. Allow users to crosspost to sites they are not added to

By default, users can crosspost only to those sub-sites of a multisite network they are added, and more than that, they should have a minimum required capability on those sites.

But you can completely skip that part with this small code snippet:

add_filter( 'rudr_smc_allowed_blog_ids', function( $allowed_blog_ids ) {

	if( ! $allowed_blog_ids = get_site_option( 'cms_blogs' ) ) {
		$allowed_blog_ids = get_sites( array(
			'site__not_in' => get_current_blog_id(),
			'fields' => 'ids',
		) );
	}
	return $allowed_blog_ids;

} );

rudr_smc_auto_mode_blog_ids

Allows you to modify the list of blog IDs where a post will be automatically crossposted to when the “Auto Mode” option is activated.

$blog_ids = apply_filters( 'rudr_smc_auto_mode_blog_ids', $blog_ids, $post_id );
$blog_idsint[]
An array of blog IDs we are publishing to.
$post_idint
An ID of an initial post (or a WooCommerce product).

Examples of using this filter hook can be found here.

rudr_smc_max_sites

By default, the plugin displays a maximum of 100 subsites at once. You can change this number with this hook.

add_filter( 'rudr_smc_max_sites', function( $number ) {
	return 150;
} );

rudr_smc_sites_orderby

By default, the sites are ordered by their ID, but with this hook, you can change it, for example, to display the sites in alphabetical order. Supports all the values that are supported by the get_sites() function. You can find them here.

add_filter( 'rudr_smc_sites_orderby', function( $orderby ) {
	$orderby = 'domain';
	return $orderby;
} );

There is also the rudr_smc_sites_order hook, which works similarly.

rudr_smc_required_capability

Allows you to change the WordPress capability users should have on a specific site in order to crosspost to it.

$capability = apply_filters( 'rudr_smc_required_capability', $capability );
$capabilitystring
A required capability users should have on a website (except super administrators) in order to have an ability to crosspost to it. Default – publish_posts.

Example. Allow crossposting only if a user is added to a site as an administrator

add_filter( 'rudr_smc_required_capability', function( $capability ) {
	$capability = 'administrator';
	return $capability;
} );

rudr_smc_allow_from

In this hook, you can provide some conditions in order to allow/disallow crossposting from a specific blog.

$allow = apply_filters( 'rudr_smc_allow_from', $allow, $original_blog_id, $post_id );
$allowbool
  • true – crossposting is allowed from this site,
  • false – crossposting is not allowed from this site.
$original_blog_idint
The site ID where the post is going to be published from.
$post_idint
The post ID.

The hook is running on the original site.

site_option_rudr_smc_post_types

It is just a default WordPress hook, which is documented here, and it can be super-useful when you work with my multisite crossposting plugin.

The hook is running on the original site.

Example. Allow specific post types for crossposting depending on a site ID

add_filter( 'site_option_rudr_smc_post_types', function( $post_types ) {

	$blog_id = get_current_blog_id();

	if( 2 === $blog_id ) {
		// only allow to crosspost posts and pages from Site 2
		$post_types = array( 'post', 'page' );
	}

	if( 3 === $blog_id ) {
		// only allow to crosspost posts from Site 3
		$post_types = array( 'post' );
	}

	if( 5 === $blog_id ) {
		// never allow to crosspost pages from Site 5
		if ( ( $key = array_search( 'page', $post_types ) ) !== false ) {
			unset( $post_types[ $key ] );
		}
	}

	return $post_types;

} );

rudr_smc_use_domains_as_names

By default, the site names are displayed in WordPress admin in my plugin UI. But in some cases, you may have multiple websites with the same name, and you would like the domains to be displayed instead. Which is also possible with a hook.

add_filter( 'rudr_smc_use_domains_as_names', '__return_true' );

The code allows the sites to be displayed like:

display domains instead of site names

rudr_smc_do_checkboxes

By default, the checkboxes on the blogs where you’re crossposting to are checked.

It means when you’re crossposting a post to Blog 2 and Blog 3, then once the post appears there, the checkboxes in the “Publish on” section near all the blogs where this post has copies are checked by default.

But you can ask the plugin not to check them automatically with a single line of code:

add_filter( 'rudr_smc_do_checkboxes', '__return_false' );

rudr_wc_crosspost_allow_new_products

Prevents the plugin from creating new WooCommerce products on sub-stores. Only if a product with the same SKU exists then it is going to be updated.

$allow = apply_filters( 'rudr_wc_crosspost_allow_new_products', $allow, $store_id );
$allowbool
  • true – creating new products is allowed,
  • false – creating new products isn’t allowed.
$store_idint
The site ID where the product is going to be published to.

The hook is running when switched to a target site.

Example 1. Only allow updating existing products

add_filter( 'rudr_wc_crosspost_allow_new_products', '__return_false' );

Example 2. Only allow updating existing products for specific stores

add_filter( 'rudr_wc_crosspost_allow_new_products', function( $allow, $store_id ) {
	
	// we do not create products, only update them on Store 3
	if( 3 === $store_id ) {
		$allow = false;
	}
	
	return $allow;
	
}, 20, 2 );

rudr_pre_crosspost_post_data

Allows you to modify any post data before publishing or updating it on another site.

$data = apply_filters( 'rudr_pre_crosspost_post_data', $data, $blog_id, $action );
$dataarray
Contains all the post data which was prepared before switching to a target site.
$blog_idint
The site ID where the post is going to be published to.
$actionstring
  • publish – when creating a new post,
  • update – when a post has already been crossposted and currently is being updated.

The hook is running on the original site.

Example 1. Change a post type when crossposting to a specific site

add_filter( 'rudr_pre_crosspost_post_data', function( $data, $blog_id ){

	if( 3 === $blog_id ) {
		$data[ 'post_data' ][ 'post_type' ] = 'page';
		// everything that is crossposting to blog ID 3 will have "page" post type
	}
	return $data;

}, 20, 2 );

Example 2. Replace initial site URLs with the target site URLs in the post content

add_filter( 'rudr_pre_crosspost_post_data', function( $data, $blog_id ){

	// get both sites information
	$site_1 = get_site();
	$site_2 = get_site( $blog_id );

	if( ! empty( $data[ 'post_data' ][ 'post_content' ] ) ) {
		$data[ 'post_data' ][ 'post_content' ] = str_replace( $site_1->domain . $site_1->path, $site_2->domain . $site_2->path, $data[ 'post_data' ][ 'post_content' ] );
	}
	return $data;

}, 10, 2 );

Example 3. Publish new posts but do not update them

It is possible to force prevent any post updates from being synced.

add_filter( 'rudr_pre_crosspost_post_data', function( $data, $blog_id, $action ){
	
	if( 'update' === $action ) {
		// if we're updating a post, return an empty post data array
		return array();
	} {
		// otherwise return original post data
		return $data;
	}

}, 20, 3 );

rudr_pre_crosspost_product_data

Modify any WooCommerce product data before publishing or updating it on another store.

$data = apply_filters( 'rudr_pre_crosspost_product_data', $data, $store_id );
$dataarray
Contains all the product data which was prepared before switching to a target site.
$store_idint
The store ID where the product is going to be published to.

Example. Replace initial store URLs with the target store URLs in descriptions

add_filter( 'rudr_pre_crosspost_product_data', function( $data, $store_id ){

	$store_1 = get_site();
	$store_2 = get_site( $store_id );

	$data[ 'description' ] = str_replace( $store_1->domain . $store_1->path, $store_2->domain . $store_2->path, $data[ 'description' ] );

	return $data;

}, 10, 2 );

rudr_pre_crosspost_content

Allows you to modify post content before publishing on another site.

$content = apply_filters( 'rudr_pre_crosspost_content', $content, $blog_id );
$contentstring
The post content.
$blog_idint
The site ID where the post is going to be published to.

The hook is running on the original site.

rudr_pre_crosspost_attachment_data

With this filter hook, you can modify attachment data before crossposting it.

$data = apply_filters( 'rudr_pre_crosspost_attachment_data', $data );

The hook is running on the original site.

The hook is used by my other plugin – Multisite Shared Media Library (for example, by default, the crossposting plugin copies featured images to subsites, but if you don’t need it, you can use a shared media library for your multisite network).

rudr_pre_crosspost_terms

Allows you to add or change taxonomy terms on a crossposted copy of a post.

$terms = apply_filters( 'rudr_pre_crosspost_terms', $terms, $blog_id );
$termsarray[]
An array of terms to assign to a post where taxonomy names are keys, and values are arrays of term slugs of this specific taxonomy.
$blog_idint
The site ID where the post is going to be published to.

The hook is running when switched to a target site.

Example. Add a specific category when crossposting to a site with ID = 3 only

add_filter( 'rudr_pre_crosspost_terms', function( $terms, $blog_id ){

	if( 3 === $blog_id ) {

		$terms = array(
			'category' => array( 'my-category-123' )
		);

	}
	return $terms;

}, 20, 2 );

Check more examples in this article.

rudr_crosspost_publish

Fires when a post has been published on a sub-site.

do_action( 'rudr_crosspost_publish', $new_post_id, $blog_id, $data );
$new_post_idint
ID of the newly published post.
$blog_idint
The blog ID where the post has just been published.
$dataarray
Contains all the data of the original post.

The hook is running when switched to a target site.

rudr_crosspost_update

Fires when a post has been updated on a sub-site.

do_action( 'rudr_crosspost_update', $post_id, $blog_id, $data );
$post_idint
ID of the updated post.
$blog_idint
The blog ID where the post has just been updated.
$dataarray
Contains all the data of the original post.

The hook is running when switched to a target site.

rudr_smc_crosspost

Fires when the process of crossposting of a specific post (or a WooCommerce product) is fully finished.

do_action( 'rudr_smc_crosspost', $post_id, $blog_ids );
$post_idint
ID of the crossposted post.
$blog_idsint[]
The array of blog ID where the post has just been crossposted.

The hook is fired from the initial site.

Need more help?