Simple WordPress Crossposting: Hook Reference

Below you will find the documentation of action and filter hooks included into the plugin that allow you to make the plugin super-customised for your needs.

rudr_swc_pre_crosspost_post_data

This hook allows to modify almost any post data before it has been crossposted to another site. For example you may want to search and replace the domain name in the post content:

add_filter( 'rudr_swc_pre_crosspost_post_data', function( $post_data, $blog ) {
	
	$post_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
		$post_data[ 'content' ]
	);
	
	return $post_data;
	
}, 10, 2 );
$post_data
(array) Post data.
$blog
(array) Stores all the information about the site we’re currently crossposting to. You can print_r() it to see what is inside.
$hook_action
(string) Can be create or update depending on whether this post is being updated or created at the moment.

rudr_swc_pre_crosspost_product_data

When you’re about to crosspost WooCommerce products, you can use this hook to exclude or modify some product data, store-specific.

For example you can ask the plugin to ignore and not transfer the product visibility statuses to other stores:

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $product_data, $blog_id, $hook_action ) {
	// only for updates
	if( 'update' === $hook_action ) {
		unset( $product_data[ 'catalog_visibility' ] );
	}
	return $product_data;
}, 10, 3 );

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.

It is also possible to add store-specific conditions, by default it applies for all stores.

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $product_data, $blog_id, $hook_action ) {
	// only for updates
	// $blog_id is usually a store domain without https:// and slashes
	if( 'update' === $hook_action && 'store.rudrastyh.com' === $blog_id ) {
		unset( $product_data[ 'catalog_visibility' ] );
	}
	return $product_data;
}, 10, 3 );
$product_data
(array) Product data.
$blog_id
(string) It is usually a store domain without https:// and slashes.
$hook_action
(string) Can be create or update depending on whether this product is being updated or created at the moment.

Another example – how to automatically change prices when crossposting products:

rudr_swc_pre_crosspost_variation_data

This hook is similar to rudr_swc_pre_crosspost_product_data, but works for product variations as you could’ve guessed. The both hook arguments are the same.

add_filter( 'rudr_swc_pre_crosspost_variation_data', function( $product_data, $blog_id, $hook_action ) {
	// only for variation updates
	if( 'update' === $hook_action ) {
		unset( $product_data[ 'catalog_visibility' ] );
	}
	return $product_data;
}, 10, 3 );

rudr_swc_pre_crosspost_meta

This filter allows to modify a meta field before its value is sent to a target website.

Below is an example how we can automatically change the meta value to an original post URL when crossposting it to Site 2. But please keep in mind that this filter hook doesn’t adds a meta field, it just changes its value, so it should exist for a post on Site 1 with any value anyway.

add_filter( 'rudr_swc_pre_crosspost_meta', function( $meta_value, $meta_key, $post_id ) {
	
	if( 'meta_key_1' === $meta_key ) {
		$meta_value = 'It is a crossposted post: ' . get_permalink( $post_id );
	}
	
	return $meta_value;
	
}, 10, 3 );

rudr_swc_taxonomy_destination

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

add_filter( 'rudr_swc_taxonomy_destination', 'rudr_change_terms', 20, 2 );

function rudr_change_terms( $terms, $blog_url ){

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

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

	}
	return $terms;

}
$terms
(array) Associative array of taxonomy rest base=>array(term IDs on Site 2) of the original post (or slugs if you are about to crosspost to wordpress.com website). If you don’t know what “taxonomy rest base” is, you can try to use a taxonomy name instead, in most cases they match.
$blog_url
(string) The blog URL where the post is going to be crossposted to.

Need more help?