jtsternberg / Taxonomy_MetaData

WordPress Helper Class for saving pseudo-metadata for taxonomy terms
54 stars 10 forks source link

Documentation still shows old way of adding fields? #25

Closed austinjreilly closed 9 years ago

austinjreilly commented 9 years ago

Per the gitter chat exchange we had a little while ago, fields should be added using a method instead of as an array. I think this implies that the syntax for registering a metabox should be:

function cmb2_taxonomy_meta_initiate() {

    require_once( 'CMB2/init.php' );
    require_once( 'Taxonomy_MetaData/Taxonomy_MetaData_CMB2.php' );

    $prefix = '_cat_options_';

    /**
     * Semi-standard CMB2 metabox/fields array
     */
    $cat_options_meta_box = new_cmb2_box(array(
        'id'         => $prefix . 'metabox',
        // 'key' and 'value' should be exactly as follows
        'show_on'    => array( 'key' => 'options-page', 'value' => array( 'unknown', ), ),
        'show_names' => true, // Show field names on the left
    ));

    $cat_options_meta_box->add_field(array(
        'name' => __( 'Category Archive Featured Post', 'taxonomy-metadata' ),
        'desc' => __( 'Enter Post ID', 'taxonomy-metadata' ),
        'id'   => 'feat_post', // no prefix needed since the options are one option array.
        'type' => 'text',
    ));

    $cat_options_meta_box->add_field(array(
        'name' => __( 'Test Checkbox', 'taxonomy-metadata' ),
        'desc' => __( 'field description (optional)', 'taxonomy-metadata' ),
        'id'   => 'test_checkbox',
        'type' => 'checkbox',
    ));

    $cat_options_meta_box->add_field(array(
        'name' => __( 'Test Text Small', 'taxonomy-metadata' ),
        'desc' => __( 'field description (optional)', 'taxonomy-metadata' ),
        'id'   => 'test_textsmall',
        'type' => 'text_small',
        // 'repeatable' => true,
    ));

    $cat_options_meta_box->add_field(array(
        'name'    => __( 'Test wysiwyg', 'taxonomy-metadata' ),
        'desc'    => __( 'field description (optional)', 'taxonomy-metadata' ),
        'id'      => 'test_wysiwyg',
        'type'    => 'wysiwyg',
        'options' => array( 'textarea_rows' => 5, ),
    ));

    // (Recommended) Use wp-large-options
    require_once( 'wp-large-options/wp-large-options.php' );
    $overrides = array(
        'get_option'    => 'wlo_get_option',
        'update_option' => 'wlo_update_option',
        'delete_option' => 'wlo_delete_option',
    );

    /**
     * Instantiate our taxonomy meta class
     */
    $cats = new Taxonomy_MetaData_CMB2( 'category', $cat_options_meta_box->metabox, __( 'Category Settings', 'taxonomy-metadata' ), $overrides );
}
cmb2_taxonomy_meta_initiate();

Is that correct?

mrtwebdesign commented 9 years ago

I would love to see an update for this. I tried my own conversion to the new method, but it's not working. I'm still getting errors. [01-May-2015 21:40:42 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'Invalid CMB2 property: metabox' in /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/cmb2/includes/CMB2.php:941

austinjreilly commented 9 years ago

@mrtwebdesign Could I/we take a look at your code? Mine works as far as I can tell.

mrtwebdesign commented 9 years ago

@austinjreilly

Here is my code.. I keep my metaboxes in a separate file. I've included all of my CMB2 code. The metabox and test text field for my taxonomy is at the end.

<?php
/**
 *
 * @category BeastTV
 * @package  Metaboxes
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     https://github.com/WebDevStudios/CMB2
 */```

/**
 * Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
 */

if ( file_exists( dirname( __FILE__ ) . '/cmb2/init.php' ) ) {
    require_once dirname( __FILE__ ) . '/cmb2/init.php';
} elseif ( file_exists( dirname( __FILE__ ) . '/CMB2/init.php' ) ) {
    require_once dirname( __FILE__ ) . '/CMB2/init.php';
}

add_action( 'cmb2_init', 'beast_register_demo_metabox' );
/**
 * Hook in and add a demo metabox. Can only happen on the 'cmb2_init' hook.
 */
function beast_register_demo_metabox() {

    // Start with an underscore to hide fields from custom fields list
    $prefix = '_beast_';

    /**
     * Sample metabox to demonstrate each field type included
     */
    $beast_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => __( 'Test Metabox', 'cmb2' ),
        'object_types'  => array( 'page','post' ), // Post type
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true, // Show field names on the left
        // 'cmb_styles' => false, // false to disable the CMB stylesheet
        // 'closed'     => true, // true to keep the metabox closed by default
        ) );

    $beast_demo->add_field( array(
        'name' => __( 'Test Text', 'cmb2' ),
        'desc' => __( 'field description (optional)', 'cmb2' ),
        'id'   => $prefix . 'text',
        'type' => 'text_small',
        // 'repeatable' => true,
        ) );

// here we start the real ones. Remove only the 2 above.

    $beast_user_city = new_cmb2_box( array(
        'id'            => $prefix . 'user_city',
        'title'         => __( 'Cities', 'cmb2' ),
        'object_types'  => array('user'), // Post type
        'context'       => 'side',
        'priority'      => 'default',
        'show_names'    => true, // Show field names on the left
        'new_user_section' => 'add-new-user', // where form will show on new user page. 'add-existing-user' is only other valid option.
        // 'cmb_styles' => false, // false to disable the CMB stylesheet
        // 'closed'     => true, // true to keep the metabox closed by default
        ) );

    $beast_user_city->add_field( array(
        'name'     => __( 'Custom info from theme', 'cmb2' ),
        'desc'     => __( 'field description (optional)', 'cmb2' ),
        'id'       => $prefix . 'user_info',
        'type'     => 'title',
        'on_front' => false,
        ) );

    $beast_user_city->add_field( array(
        'name'     => __( 'Taxonomy Multi Checkbox', 'cmb2' ),
        'desc'     => __( 'field description (optional)', 'cmb2' ),
        'id'       => $prefix . 'multitaxonomy',
        'type'     => 'taxonomy_multicheck',
        'taxonomy' => 'city', // Taxonomy Slug
        // 'inline'  => true, // Toggles display to inline
        ) );

    require_once( 'Taxonomy_MetaData/Taxonomy_MetaData_CMB2.php' );

    $beast_city_data = new_cmb2_box( array(
        'id'            => $prefix . 'city_data',
        'show_on'    => array( 'key' => 'options-page', 'value' => array( 'unknown', ), ),
        'show_names'    => true, // Show field names on the left
        ) );

    $beast_city_data->add_field( array(
        'name' => __( 'Test Text', 'cmb2' ),
        'desc' => __( 'field description (optional)', 'cmb2' ),
        'id'   => $prefix . 'city_data_text',
        'type' => 'text',
        ) );

    /**
     * Instantiate our taxonomy meta class
     */
    $cats = new Taxonomy_MetaData_CMB2( 'category', $beast_city_data->metabox, __( 'Category Settings', 'taxonomy-metadata' ), $overrides );

  }

Here is the error I'm getting:

[01-May-2015 22:37:23 UTC] PHP Fatal error:  Uncaught exception 'Exception' with message 'Invalid CMB2 property: metabox' in /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/cmb2/includes/CMB2.php:941
Stack trace:
#0 /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/functions-metaboxes.php(105): CMB2->__get('metabox')
#1 [internal function]: beast_register_demo_metabox('')
#2 /Users/Nomad/htdocs/beast/wp-includes/plugin.php(496): call_user_func_array('beast_register_...', Array)
#3 /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/cmb2/bootstrap.php(8): do_action('cmb2_init')
#4 /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/cmb2/init.php(106): require_once('/Users/Nomad/ht...')
#5 [internal function]: cmb2_bootstrap_205->include_cmb('')
#6 /Users/Nomad/htdocs/beast/wp-includes/plugin.php(496): call_user_func_array(Array, Array)
#7 /Users/Nomad/htdocs/beast/wp-settings.php(353): do_action('init')
#8 /Users/Nomad/htdocs/beast/wp-config.php(96): require_once('/Users/Nomad/ht...')
#9 /Users/Nomad/htdocs/beast/wp-load.php(37): require_once(' in /Users/Nomad/htdocs/beast/wp-content/themes/beast.tv/cmb2/includes/CMB2.php on line 941

Thanks for taking a look. I've gotten to the point where I'll take a look at my code tomorrow and hope my mistakes will magically be obvious. I may try cmb2-taxonomy tomorrow if I can add it to my theme. I try to keep anything my themes depend on for functionality, built-in.

austinjreilly commented 9 years ago

The first thing I would try is to separate out your metaboxes into different functions, and separately add hooks to cmb2_init. That's the way it's done in the example functions file.

That error message seems to indicate to me that the $beast_city_data object doesn't contain a metabox property, which is why it isn't working.

Also, I have an issue open in the cmb2-taxonomy repo to hopefully get some kind of clarification as to why I would use that plugin as opposed to this helper class, so we'll see if anything comes of that.

BeardedGinger commented 9 years ago

Just implemented this way. It looks like the issue is due to the function being "meta_box" instead of "metabox".

If you change: $cats = new Taxonomy_MetaData_CMB2( 'category', $beast_city_data->metabox, __( 'Category Settings', 'taxonomy-metadata' ), $overrides ); to: $cats = new Taxonomy_MetaData_CMB2( 'category', $beast_city_data->meta_box, __( 'Category Settings', 'taxonomy-metadata' ), $overrides );

it should work.

jtsternberg commented 9 years ago

Ok folks, this has been updated, and the readme has a good demonstration for how to initiate Taxonomy_MetaData_CMB2. You can see the changes in 1.0.0 here: https://github.com/jtsternberg/Taxonomy_MetaData/compare/9e5a482e8a0fec42bf08ffb26535653b9417f832...master

jtsternberg commented 9 years ago

The original issue is resolved (using new field registration API), so I'll close this, but feel free to open another if you come across other issues.