BuddyDev

Search

Do you want global Categories, tags or taxonomies across your WordPress Multisite network?

Do you want that all of your sites in the multisite network should have same categories, tags, terms? Today, In this tutorial, we are going to make all the terms available across all blogs of a multisite. Also, a terms ( category/tags ) can be added from any of the blog. Once It is added, It will be available across all the blogs.

Let us understand some basic terminologies first.

Taxonomy: – Taxonomy is a way to classify & group together the information. What it means in WordPress, Please take a look here to understand. For example “categories”, “tags” are taxonomies which allow you to classify information( post ) in various categories/tags. In WordPress, categories, tags are inbult taxonomy while custom taxonomies can be added by using register_taxonomy as and when required.

Terms:- The name under which we group various posts etc is called term. For example, Let us say Travelling, News etc are terms where you file your posts.

How WordPress handles the taxonomies/terms on multisite:-

  • On multisite, all blogs share same database ( most of the time )
  • WordPress uses 3 tables for storing the taxonomy, terms and post to term association. These are wp_terms, wp_terms_taxonomy and wp_terms_relationships ( where wp_ is the prefix you specified at the time of installation, It may vary depending on your installation)
  • wp_terms table is used to store the actual terms ( The categories like “Awesome Food”). It contains term id, name, slug and term_group
  • wp_terms_taxonomy table is used to contain information about which terms belongs to which taxonomy.
  • wp_terms_relationships table is used to associate the terms to individual posts( or post type ).
  • Each of the blog on multisite has their own copy of these three tables.

So, all we need to do is make sure that the wp_terms & wp_terms_taxonomy table is shared across the multisite and not used per site basis. The simplest way to this is to change the property of $wpdb object as shown below. You can put the code inside mu-plugins directory.

//even before any taxonmy/terms are initialized, we reset the tables
add_action( 'init', 'buddydev_change_tax_terms_table', 0 );
//on blog switching, we need to reset it again, so it does not use current blog's tax/terms only
//it works both on switch/restore blog
add_action( 'switch_blog', 'buddydev_change_tax_terms_table', 0 );

function buddydev_change_tax_terms_table(){
    global $wpdb;
    //change terms table to use main site's
    $wpdb->terms = $wpdb->base_prefix . 'terms';
    //change taxonomy table to use main site's taxonomy table
    $wpdb->term_taxonomy = $wpdb->base_prefix . 'term_taxonomy';
    //if you want to use a different sub sites table for sharing, you can replca e$wpdb->vbase_prefix with $wpdb->get_blog_prefix( $blog_id )
}

Now, The terms and terms_taxonomy table of the main site is shared  with all the blogs, so all the terms from main site will appear everywhere. Also, If a new term is added from any of the blog, It will appear on all other sites.

Issues:-

  • If there are existing terms on sub blogs which are not on the main site. They will be lost.
  • If you are using custom taxonomies, the terms belonging to custom taxonomies will be only available if the taxonomy is registered on the sub site( on which you want to access it )

If you need it as a plugin, Here I have put it as a plugin:-

Download:-

http://buddydev.com/plugins/mu-global-terms/

The plugin must be network activated.

PS: We are in the process of updating our old plugins and putting some new as promised in the previous post. Please do let us know your ideas/suggestions in the comment below.

Get latest WordPress & BuddyPress tutorials, news and tricks in your inbox.

We send out a lovely email newsletter with the most popular stories. Be a part of it.

5 Responses to Do you want global Categories, tags or taxonomies across your WordPress Multisite network?

Leave a Reply

Are you looking for support?

Please join BuddyDev Premium Club to get timely support. At the moment, We are unable to provide much support in comments. By Joining BuddyDev Premium Club, you will get guaranteed support(and we have 30 days money back guarantee too).

Your email address will not be published. All the fields marked as * are required.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>