0

I'm using a custom taxonomies across a Wordpress multisite install (same taxonomies on all sites) and i'm trying to add some custom meta boxes to each of these (same meta boxes on all sites).

I'm using the following plugin to activate a global meta tags and add the appropriate table in the database: http://wordpress.org/extend/plugins/simple-meta-tags/

So far I also have the following code:

add_action('taxonomyname_add_form_fields', 'taxonomyname_metabox_add', 10, 1);
add_action('taxonomyname_edit_form_fields', 'taxonomyname_metabox_edit', 10, 1);    

function taxonomyname_metabox_add($tag) { ?>
<h3>Meta Box Info Title</h3>
    <div class="form-field">
        <label for="extrafield1"><?php _e('Extra Field 1') ?></label>
        <input name="extrafield1" id="extrafield1" type="text" value="" size="40" aria-required="true" />
    </div>
    <div class="form-field">
        <label for="extrafield2"><?php _e('Extra Field 2') ?></label>
        <input name="extrafield2" id="image2" type="text" value="" size="40" aria-required="true" />
    </div>
<?php }     

function taxonomyname_metabox_edit($tag) { ?>
<h3>Meta Box Info Title</h3>
    <table class="form-table">
        <tr class="form-field">
        <th scope="row" valign="top">
            <label for="extrafield1"><?php _e('Extra Field 1'); ?></label>
        </th>
        <td>
            <input name="extrafield1" id="extrafield1" type="text" value="<?php echo get_term_meta($tag->term_id, 'extrafield1', true); ?>" size="40" aria-required="true" />
        </td>
        </tr>
        <tr class="form-field">
        <th scope="row" valign="top">
            <label for="extrafield2"><?php _e('Extra Field 2'); ?></label>
        </th>
        <td>
            <input name="extrafield2" id="extrafield2" type="text" value="<?php echo get_term_meta($tag->term_id, 'extrafield2', true); ?>" size="40" aria-required="true" />
        </td>
        </tr>
    </table>
<?php }



add_action('created_taxonomyname', 'save_taxonomyname_metadata', 10, 1);    
add_action('edited_taxonomyname', 'save_taxonomyname_metadata', 10, 1);

function save_taxonomyname_metadata($term_id){
{

    if (isset($_POST['extrafield1'])) 
        update_term_meta( $term_id, 'extrafield1', $_POST['extrafield1']);                  
}
{
    if (isset($_POST['extrafield2'])) 
        update_term_meta( $term_id, 'extrafield2', $_POST['extrafield2']);                  
}
}

The meta boxes display fine across all sites, and save ok on the main site, but on sub-sites no inputs will save. I assume this is because no blog_id or global variable is declared in the save function, but i'm a bit confused how to add/do this.

Can anyone assist please?

1 Answer 1

1

Turns out that the function above actually works fine.

The issue is that the plugin (Simple Terms Meta) doesn't create tables for each site, only the main site.

I've managed to modify the plugin so that when it's activated it now creates a table for each site and it appears to be working fine and saving details for each site now.

If anyone else needs to do the same, replace lines 31-59 of the plugin (Simple Terms Meta) with the following code (and of course use the functions above as a basis for creating your own meta boxes for taxonomies). I'd also advise anyone attempting this to read this article for guidance on this too - http://www.wpmods.com/adding-metadata-taxonomy-terms/

/* Network Activation to Install Tables on all Sites */
register_activation_hook( __FILE__, 'simple_term_meta_install' );
function simple_term_meta_install() {
    global $wpdb;

    if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
                    $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                _simple_term_meta_install();
            }
            switch_to_blog($old_blog);
            return;
        }   
    } 
    _simple_term_meta_install();        
}

/* End Network Activation to Install Tables on all Sites*/

/* Individual Activation to Install on Selected Sites */
function _simple_term_meta_install() {
    // Create term metadata table if necessary
    global $wpdb;
        $table_name = $wpdb->prefix . $type . 'termmeta';
    if ($wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'") != $table_name) {
        simple_term_meta_table_creation($table_name, $type);
        }
}
/* End Individual Activation to Install on Selected Sites */

function simple_term_meta_table_creation()
{
    // setup custom table

    global $wpdb;
    $table_name = $wpdb->prefix . 'termmeta';
    if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) :

        $sql = "CREATE TABLE " . $table_name . " (
          meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
          term_id bigint(20) unsigned NOT NULL DEFAULT '0',
          meta_key varchar(255) DEFAULT NULL,
          meta_value longtext,
          PRIMARY KEY (meta_id),
          KEY term_id (term_id),
          KEY meta_key (meta_key)     
        );";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);

    endif;

    update_option( "simple_term_meta_db_version", '0.9' );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.