Simple Multisite Crossposting: Hook Reference

From time to time, I am asked to add some functionality to the plugin. Let’s say 1 of 50 customers wants something custom specifically for the own project.

But as you can see from the plugin name “Simple Multisite Crossposting” where the key word is “simple”, my goal is to keep my plugin from bloated code as far away as possible.

Luckily, most of the requested functionality can be implemented with WordPress hooks.

Using hooks

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

Action and Filter Hook Reference

rudr_crosspost_post_type_destination

Allows you to change CPT (custom post type) while crossposting a post.

add_filter( 'rudr_crosspost_post_type_destination', 'rudr_change_post_type', 20, 3 );

function rudr_change_post_type( $post_type, $blog_id, $data ){

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

}
$post_type
(string) The post type of the original post.
$blog_id
(integer) The blog ID where the post is going to be crossposted to.
$data
(array) Contains all the data of the post.

rudr_crosspost_taxonomy_destination

Allows to change post’s taxonomy terms while crossposting it.

add_filter( 'rudr_crosspost_taxonomy_destination', 'rudr_change_terms', 20, 3 );

function rudr_change_terms( $terms, $blog_id, $data ){

	if( 3 === $blog_id ) {

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

	}
	return $terms;

}
$terms
(array) Associative array of taxonomy=>array(term slugs) of the original post.
$blog_id
(integer) The blog ID where the post is going to be crossposted to.
$data
(array) Contains all the data of the post.

rudr_crosspost_to_status

Allows to change post statuses after crossposting.

add_filter( 'rudr_crosspost_to_status', 'rudr_copy_as_draft', 20, 4 );

function rudr_copy_as_draft( $post_status, $post_type, $blog_id, $update ){
	return 'draft';
}
$post_status
(string) New post status name.
$post_type
(string) Post type name.
$blog_id
(integer) The blog ID where the post is going to be crossposted to. This parameter is always 0 for product variations.
$update
(bool) Updating an existing post or creating a new one?.

rudr_crosspost_check_caps

By default if a user can edit a specific post on one blog, he/she also can crosspost it to any of the blogs available for crossposting.

But with this hook you can also add a blog-related capability check. You can specify a specific capability (the full list is here) the user should have on other blogs in order to post to them.

For example if you use a snippet below, the user can crosspost a post only to sites where he can publish and edit posts.

add_filter( 'rudr_crosspost_check_caps', function( $capability ) {
	$capability = 'edit_posts'; // the capability name or role name here to check
	return $capability;
} );

rudr_crosspost_allowed_from

Allows to set some custom rules in case you decide to disallow crossposting from a specific blog.

For example with the help of code snippet below we can prevent only the updates made on site with ID = 2 from being posted to the original articles.

add_filter( 'rudr_crosspost_allowed_from', function( $is_allowed, $blog_id, $post_id ) {

	// only for blog with ID = 2
	if( 2 !== $blog_id ){
		return $is_allowed;
	}
	
	// let's check if it is NOT original article, we can do that by checking guid against the site url
	$post = get_post( $post_id );
	
	if( $post && $post->guid !== site_url() . '/?p=' . $post->ID ) {
		// no original, disallow crosspost
		$is_allowed = false;
	}

	return $is_allowed;

}, 10, 3 );

rudr_smc_do_checkboxes

By default the checkboxes on the blogs where you’re crossposting to are checked. I mean when you’re crossposting an article 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_crosspost_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_crosspost_use_domains_as_names', '__return_true' );

The code allows the sites to be displayed like:

display domains instead of site names

rudr_crosspost_max_sites

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

add_filter( 'rudr_crosspost_max_sites', function( $max ) {
	return 50;
} );

rudr_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_crosspost_ignore_images', '__return_true' );

rudr_wc_crosspost_allow_new_products

This filter hook prevents the plugin from creating new WooCommerce products on sub-stores. Only if a products with the same SKU is found, then it is going to be updated.

Completely prevents creating new products, only updating existing ones.

add_filter( 'rudr_wc_crosspost_allow_new_products', '__return_false' );

You can also turn on “Update mode” for specific WooCommerce 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;
	
} 

site_option_rudr_smc_post_types

This filter hook allows you to allow or disallow specific post types from crossposting. You can also add site-specific conditions as on the example below:

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;

} );

Need more help?