I'm attempting to setup a MultiSite network, and want the same structure across the sites. This includes Categories.
I've already got code that duplicates Posts - but discovered that if the original post was assigned a category that didn't exist on the other sites in the network,
WP would just "create" a category with the same name (but ignore description, parent etc.).
I've tried to following function - but see no creation of category on other sites in the network.
function category_to_all( $term_id, $tt_id ){
remove_action( 'create_category', __FUNCTION__ );
$currentcatdata = get_term_by('id', $id, 'category');
// Get & Set Category data ready
$categorydata = array(
'cat_ID' => $term_id,
'taxonomy' => 'category',
'cat_name' => $currentcatdata->name,
'category_description' => $currentcatdata->description,
'category_nicename' => $currentcatdata->slug,
'category_parent' => $currentcatdata->parent,
);
// Get all blogs/sites, excluding "current"
$allsiteids = get_sites(array('fields' => 'ids'));
$currentsiteid = get_current_blog_id();
if (($key = array_search($currentsiteid, $allsiteids)) !== false) {
unset($allsiteids[$key]);
}
$blog_ids = array_values($allsiteids);
foreach( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
//create category?
wp_insert_term($categorydata['cat_name'], 'category',
array(
'description' => $categorydata['category_description'],
'slug' => $categorydata['category_nicename'],
'parent'=> $categorydata['category_parent']
)
);
restore_current_blog();
}
}
add_action( 'create_category', 'category_to_all', 10, 2 );
The idea is that whenever anyone creates a new category,
it should be duplicated across the other sites (so new posts can go to the corresponding category per-site).
Any pointers or help would be appreciated - TY.