I want to create “city” taxonomy in all sites of MultiSite. But i want to show same listing in every blog and i want to edit it from only master site and not letting other blog owners edit it. I think only solution for this is setting a master site for specific taxonomy in MultiSite. So when someone try to use “city” taxonomy from other blogs, they will see master site’s city list. How can i do that?
<?php
$master_site = 1;
...when city taxonomy need:
switch_to_blog( $master_site );
...use list of "city" taxonomy"...
restore_current_blog();
First when you register your custom taxonomy use the capabilities argument and define your custom capabilities:
and only give your users the
assign_terms
capability this way they wont be able to create new terms, only YOU.Then use the nice solution you linked in the comment ,
change the
$taxonomies_to_sync
array according to your taxonomy, and you should be fine.(disclaimer: this involves a core WP hack, tread carefully)
I have a Multisite network where I wanted the primary blog (id == 1) to set the taxonomy
activity_type
for all other sites on the network.I used the code below to include hooks when creating, editing or deleting terms in that custom taxonomy which then mimics that task on all subsites. What is not covered here is restricting users on other sites from adding/editing/deleting terms – I used a combination of jQuery, CSS and
remove_action
functions to hide those UI elements from my subsites.This also requires a hack to WP core (yes, beware of this carinal sin for it is a terrible transgression). In
wp-includes/taxonomy.php
find line 1767 or the$term = (int) $term;
line within thewp_delete_term
function. There, place this line:I might also leaving yourself a comment here and also tattooing a reminder of this change somewhere because you will have to manually update
taxonomy.php
every time you update WordPress. This sucks but the reason we’re setting this transient is so we can lookup the deleted term by its slug rather than its ID. The action hookdelete_{tax_type}
gives us the term_id for the deleted term but we can’t be sure that will be the same on all of our network sites, so we have to get the term slug before it is deleted… thus the core hack. I don’t love it, I’m not proud of it, but it works. If you only care about pushing terms to your network sites and dont care about removing them from your main site, you could actually skip this and not have to hack core – I needed to be able to create and delete from a single site and this was the only way I found.You will also notice a line in my code where this happens
$sites = as_network_ids();
. That is simply a helper function I’ve created elsewhere that returns a list of all the network blog ids along with some extra data I use for a lot of other functions. You can get the blog list using this function (warning: it is officially deprecated but hasn’t been replaced yet) or by writing your own SQL.