Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am building up a Multi Site installation of WordPress which has a custom taxonomy plugin I wrote, activated Network Wide.

I wish to modify the behaviour of the taxonomy to use a centralised list of terms from the main site so that the administrator of the main site can create, edit and remove terms, and the users of other sites on the network will have the ability to tag their posts with these.

I tried making filtering the get_terms function, this works but the post can't save these terms because they don't really exist.

I was thinking an alternative would be to sync all the blogs when changes are made to the main blog, but this will become slow as more blogs are added.

What is the best way to approach the issue, and can anyone give me some pointers...

<?php

/*

    Plugin Name: Module Taxonomy
    Version: 1.0

    Author: Tom Lynch, Oliver Smith & Luke Watts
    Author URI: https://github.com/socd-io

    Description: Creates a custom 'module' taxonomy that which shows terms created in the main site on all sub sites.

    License: GPLv3

    Copyright (C) 2013 Tom Lynch, Oliver Smith & Luke Watts

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.

*/

class SOCD_Module_Taxonomy {

    private $tax_slug = 'socd_module_taxonomy';
    private $tax_name = 'Modules';
    private $tax_singular = 'Module';

    function __construct() {
        add_action( 'init', array( &$this, 'register_taxonomy' ) );
        if ( ! is_main_site() )
            add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) );
        add_action( 'save_post', array( &$this, 'save_post' ), 1, 2 );
        add_action( 'pre_get_posts', array( &$this, 'query_posts' ) );
    }

    function register_taxonomy() {
        register_taxonomy( $this->tax_slug, array(
            'post',
            'activity'
        ), array( 
            'public' =>             true, //is_main_site() ? true : false,
            'show_in_nav_menus' =>  true,
            'show_admin_column' =>  true,
            'hierarchical' =>       true,
            'labels' => array( 
                'name' =>           $this->tax_name,
                'singular_name' =>  $this->tax_singular
            ),
            'rewrite' => array(
                'slug' => strtolower( $this->tax_singular )
            )
        ) );
    }

    function add_meta_box() {
        add_meta_box( $this->tax_slug, $this->tax_singular, array( &$this, 'output_metabox' ), 'post', 'side', 'core' );
    }

    function output_metabox( $post ) {
        global $current_site;
        $modules = get_terms( $this->tax_slug, array( 'hide_empty' => false ) );
        ?>
            <input type="hidden" name="<?php echo $this->tax_slug ?>[nonce]" value="<?php echo wp_create_nonce( $this->tax_slug ) ?>" />
            <ul style="margin-bottom: 0;" class="categorychecklist form-no-clear">
                <?php foreach( $modules as $module ): ?>
                    <li>
                        <label for="<?php echo $this->tax_slug ?>-<?php echo $module->slug ?>" class="selectit"><input value="<?php echo $module->term_taxonomy_id ?>" <?php checked( has_term( $module->name, $this->tax_slug, $post->ID ), true ) ?> type="checkbox" name="<?php echo $this->tax_slug ?>[tax][]" id="<?php echo $this->tax_slug ?>-<?php echo $module->slug ?>"> <?php echo $module->name ?></label>
                    </li>
                <?php endforeach ?>
            </ul>
        <?php
    }

    function query_posts( $query ) {
        if ( is_tax( $this->tax_slug ) && !is_admin() && $query->is_main_query() ) {

            $term_ids = get_terms( $this->tax_slug, array(
                'fields' => 'ids'
            ) );

            $tax_query = array(
                'relation' => 'OR',
                array(
                    'taxonomy'  => $this->tax_slug,
                    'field'     => 'id',
                    'terms'     => $term_ids,
                    'operator'  => 'NOT IN'
            ) );

            $query->set( 'tax_query', $tax_query );
        }
        return $query;
    }

    function save_post( $id, $post ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( ! isset( $_POST[$this->tax_slug] ) || ! wp_verify_nonce( $_POST[$this->tax_slug]['nonce'], $this->tax_slug ) ) return;
        wp_set_post_terms( $id, $_POST[$this->tax_slug]['tax'], $this->tax_slug );
    }
}

if ( class_exists( "SOCD_Module_Taxonomy" ) ) {
    $SOCD_Module_Taxonomy = new SOCD_Module_Taxonomy();
    register_activation_hook( __FILE__, 'flush_rewrite_rules' );
    register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
}
share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.