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 with 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 to 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_max_sites

By default plugin displays the 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_required_capability

Allows 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 to crosspost only if a user is added to a site as 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 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 website 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 “Publish on” section near all the blogs where this post has copies are checked by default.

But you can ask the plugin to not 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 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 );

	$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 );

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 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_ignore_images

By default plugin copies featured images to subsites. If you don’t need it, for example if you’re using a shared media library in your multisite network, then this filter hook should help you:

add_filter( 'rudr_pre_crosspost_ignore_images', '__return_true' );

The hook is running when switched to a target site.

rudr_pre_crosspost_terms

Allows 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.

Need more help?