WordPress Crossposting Hook Reference

Here you can find the documentation of filter hooks included in the plugin, which will be useful when you would like to customize the plugin for your needs.

If you don’t know where and how to use the snippets from this article, check this guide.

rudr_swc_use_domains_as_names

By default, my plugin uses Site Title from Settings > General when displaying the sites, but I completely get that sometimes it may not be convenient, so just by adding this single code line, you can display domains instead of site titles.

add_filter( 'rudr_swc_use_domains_as_names', '__return_true' );

And now… the domains are displayed instead:

display domains instead of site names

rudr_swc_allowed_sites

Allows you to modify the list of sites available for crossposting.

$blogsarray
An array with added sites.

Example. Remove a site with a specific domain from this list for everyone except administrators

add_filter( 'rudr_swc_allowed_sites', function( $sites ) {
	// we need this line to use current_user_can() function
	include_once( ABSPATH . 'wp-includes/pluggable.php' );

	if( ! current_user_can( 'administrator' ) ) {
		// remove a specific site from the list
		$sites = array_filter( $sites, function( $site ) {
			// you can also check $site[ 'name' ]
			return $site[ 'url' ] !== 'https://rudrastyh.com';
		} );

	}

	return $sites;

} );

rudr_swc_auto_mode_sites

Allows you to modify the list of sites where a post is going to be automatically crossposted to when “Auto Mode” is on. The difference from the rudr_swc_allowed_sites filter hook is that you can easily check $post_id here.

apply_filters( 'rudr_swc_auto_mode_blogs', $blogs, $post_id );
$blogsarray
An array with added sites.
$post_idint
An ID of an initial post.

Example. Exclude a specific post from auto-crossposting

add_filter( 'rudr_swc_auto_mode_blogs', function( $blogs, $post_id ) {
	
	if( 12345 == $post_id ) {
		// all we need to do is to empty the array with sites
		$blogs = array();
	}
	
	return $blogs;
	
}, 10, 2 );

More examples of using this hook can be found here.

rudr_swc_pre_crosspost_post_data

Allows to modify almost any post data before it’s been crossposted to another site.

apply_filters( 'rudr_swc_pre_crosspost_post_data', $data, $blog, $post, $action );
$dataarray
Contains all the post data which is going to be crossposted.
$blogarray
The information about the target site.
$postWP_Post
The WP_Post object of an initial post.
$actionstring
  • publish – when creating a new post,
  • update – when a post has already been crossposted and currently is being updated.

Example. Replacing the domain name in the post content

add_filter( 'rudr_swc_pre_crosspost_post_data', function( $data, $blog ) {
	
	$data[ 'content' ] = str_replace(
		site_url(), // replace the current site URLs in the post content
		$blog[ 'url' ], // with the URL of the site we're about to crosspost to
		$data[ 'content' ]
	);
	
	return $data;
	
}, 10, 2 );

rudr_swc_pre_crosspost_attachment_data

Allows to modify attachment data before it’s going to be crossposted to another site. Keep in mind that by default the plugin doesn’t transfer the attachment data; it should be allowed in the settings (“Fields” tab).

apply_filters( 'rudr_swc_pre_crosspost_attachment_data', $attachment_data, $blog, $attachment_id );
$attachment_dataarray
Contains an array of all the attachment data which is going to be crossposted. For example, you can modify the following array elements: alt_text, title, caption, description.
$blogarray
The information about the target site.
$attachment_idint
The attachment ID.

rudr_swc_pre_crosspost_terms

Allows you to change posts’ taxonomy terms while crossposting them.

$terms = apply_filters( 'rudr_swc_pre_crosspost_terms', $terms, $blog_url, $post_id );
$termsarray
The array of IDs or slugs (for wordpress.com) of post terms prepared for the current target site in the format: taxonomy rest base => array( term 1, term 2 ).
$blog_urlstring
The site URL of the current target website where the post is going to be crossposted to.
$post_idint
The post ID.

Example. Change a category and tags when crossposting to a specific site

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

function rudr_change_terms( $terms, $blog_url, $post_id ){

	if( 'https://rudrastyh.com' === $blog_url ) {

		$terms = array(
			'categories' => array( 51 ),
			'tags' => array( 10, 15, 21 ),
		);

	}
	return $terms;

}

You can find more examples in this support article.

rudr_swc_pre_crosspost_product_data

When you crosspost WooCommerce products, you can use this hook to exclude or modify some product data. Can be for specific stores only.

$data = apply_filters( 'rudr_swc_pre_crosspost_product_data', $data, $blog, $product, $action );
$dataarray
Contains all the product data which is going to be crossposted.
$blogarray
The information about the target store.
$productWP_Product
The WP_Product object of an initial product.
$actionstring
  • publish – when creating a new product,
  • update – when a product has already been crossposted and currently is being updated.

Example 1. Do not change a product visibility status when updating a product

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $data, $blog, $product, $action ) {
	// only for updates
	if( 'update' === $action ) {
		unset( $data[ 'catalog_visibility' ] );
	}
	return $data;
}, 10, 4 );

How it works – you have the same product on “Store 1” and “Store 2”. Then, if you change the product visibility status to “Hidden”, for example, on “Store 1” (with the code snippet active), it will remain unchanged on “Store 2”.

Example 2. Add a store-specific condition

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $data, $blog, $product, $action ) {
	// only for updates
	if( 'update' === $action && 'https://store.rudrastyh.com' === $blog[ 'url' ] ) {
		unset( $data[ 'catalog_visibility' ] );
	}
	return $data;
}, 10, 4 );

Example 3. How to automatically change prices when crossposting products

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $data, $blog ) {
	// only when crossposting to a specific store (optional)
	if( 'https://store.rudrastyh.com' === $blog[ 'url' ] ) {
		$data[ 'regular_price' ] = $data[ 'regular_price' ] + 10;
		// make sure to not change the data type
		$data[ 'regular_price' ] = (string) $data[ 'regular_price' ];
	}
	return $data;
}, 10, 2 );

rudr_swc_pre_crosspost_variation_data

When you crosspost WooCommerce product variations, you can use this hook to exclude or modify some product variation data

$data = apply_filters( 'rudr_swc_pre_crosspost_variation_data', $data, $blog, $variation, $action );
$dataarray
Contains all the product variation data which is going to be crossposted.
$blogarray
The information about the target store.
$variationWC_Product_Variation
The WC_Product_Variation of an initial variation.
$actionstring
  • publish – when creating a new product variation,
  • update – when a product variation has already been crossposted and currently is being updated.

rudr_swc_crosspost_publish

Fires when a post (or a WooCommerce product) has been published on the target site.

do_action( 'rudr_swc_crosspost_publish', $post_id, $blog, $new_post_id, $new_post );
$post_idint
The post ID.
$blogarray
The information about the target site.
$new_post_idint
ID of a newly published post.
$new_postarray
The post data of a newly published post, which comes together with the response.

rudr_swc_crosspost_update

Fires when a post (or a WooCommerce product) has been updated on the target site.

do_action( 'rudr_swc_crosspost_update', $post_id, $blog, $updated_post_id, $updated_post );
$post_idint
The post ID.
$blogarray
The information about the target site.
$updated_post_idint
ID of an updated post on the target site.
$updated_postarray
The post data of an updated post, which comes together with the response.

Need more help?