Crossposting from Staging to Production

You can also use my crossposting plugin in cases when you edit your content on a staging website and once ready, to push it on a live website, per post basis.

But in cases like this a little bit of extra configuration is required.

First, you will probably need to crosspost all the images in the post content. In order to do so, just go to Settings > Crosspost > Fields and activate this option:

crosspost content images plugin feature

Second, you need to replace URLs in the post content, it is easy, just use the code snippet below on your staging site (the site you’re crossposting from).

add_filter( 'rudr_swc_pre_crosspost_post_data', function( $post_data, $blog ) {
	
	// just replacing the current (stanging) site URLs with the production site URLs
	$post_data[ 'content' ] = str_replace( site_url(), $blog[ 'url' ], $post_data[ 'content' ] );
	return $post_data;
	
}, 10, 2 );

Please note, that both URLs we’re replacing are without / slashes.

If you’re using WooCommerce, you need to use rudr_swc_pre_crosspost_product_data instead of rudr_swc_pre_crosspost_post_data for products.

add_filter( 'rudr_swc_pre_crosspost_product_data', function( $product_data, $blog_id ) {
	
	$blog = Rudr_Simple_WP_Crosspost::get_blog( $blog_id );
	
	$product_data[ 'description' ] = str_replace( site_url(), $blog[ 'url' ], $product_data[ 'description' ] );
	return $product_data;
	
}, 10, 2 );

In case you have some URLs in your post meta:

add_filter( 'rudr_swc_pre_crosspost_meta', function( $value, $key, $id, $blog ) {
	
	// you can also check for specfic meta keys here and I recommend to do that
	// if( 'some_meta_key' !== $key ) {
	// 	return $value;
	// }

	$value = str_replace( site_url(), $blog[ 'url' ], $value );
	return $value;
	
}, 10, 4 );

For example, this is the snippet for Elementor specifically:

add_filter( 'rudr_swc_pre_crosspost_meta', function( $value, $key, $id, $blog ) {
	
	if( '_elementor_data' !== $key ) {
		return $value;
	}
	
	$previous_url = site_url();
	$new_url = $blog[ 'url' ];
	
	$value = str_replace( addcslashes( $previous_url, '/' ), addcslashes( $new_url, '/' ), $value );
	// if you want to make the replacements only in <a> tags, use the code below
	//$value = preg_replace_callback(
	//	'/(href=\\\?")' . preg_quote( addcslashes( $previous_url, '/' ), '/') . '(.*?)(")/',
	//	function ( $matches ) use ( $new_url ) {
	//		return $matches[1] . addcslashes( $new_url, '/' ) . $matches[2] . $matches[3];
	//	},
	//	$value
	//);

	return $value;
	
}, 10, 4 );

By the way, the snippet above will work flawlessly when you crosspost new pages with Elementor, but when you update existing ones – don’t forget about the Elementor cache. If you want the changes to be reflected immediately, go to Elementor > Tools > General and press the “Clear files and data” button.

Clear Elementor cache from the Tools page

Once again, if you don’t know where to use these code snippets.

Need more help?