Sync Taxonomies programmatically

If you have the Sync Taxonomies add-on installed, you can use PHP code to sync taxonomies programmatically.

if ( function_exists( 'broadcast_sync_taxonomies' ) )
{
	// Before you start, you're on the source blog, right?
	// This is the name of the post type the taxonomy belongs to.
	$post_type = 'post';
	// This is the taxonomy name beloning to the post type above.
	$taxonomy = 'category';
	// An array of blog IDs to which to sync the chosen taxonomy.
	$blogs = [ 5, 20, 400 ];
	broadcast_sync_taxonomies()->sync_taxonomy( $post_type, $taxonomy, $blogs );
}

You can also sync taxonomies when a new site is created.


function bc_wp_insert_site_taxonomy_sync( $data )
{
	// Config starts.
	
	// Which post types, and in then, which taxonomies, do you wish to sync?
	$types_and_taxonomies = [
		'post' => [				// The "post" post type.
			'category',			// Taxonomy to sync.
			'post_tag',			// Taxonomy to sync.
		],
		'people' => [			// A custom post type called "people".
			'job',				// Taxonomy to sync.
			'workplace',		// Taxonomy to sync.
		],
	];
	// Which blog do you wish to sync FROM?
	$parent_blog = 1;
	
	// End of config.
	
	if ( ! function_exists( 'broadcast_sync_taxonomies' ) )
		return;
	$new_blog_id = $data->blog_id;
	
	switch_to_blog( $parent_blog );
	foreach( $types_and_taxonomies as $post_type => $taxonomies )
		foreach( $taxonomies as $taxonomy )
			broadcast_sync_taxonomies()->sync_taxonomy( $post_type, $taxonomy, [ $new_blog_id ] );
	restore_current_blog();
}
add_action( 'wp_initialize_site', 'bc_wp_insert_site_taxonomy_sync', 1000 );