bu-ist / bu-versions

Make and save edits to published posts/pages in WordPress without immediately replacing the public content.
https://developer.bu.edu/bu-versions/
19 stars 5 forks source link

Adding Post Meta Support for Alternate Versions #5

Closed coeweb closed 11 years ago

coeweb commented 11 years ago

First of all, I think I can piggy-back on many of the kudos to your plugin that I've come across - this is a VERY useful plugin that solves a massive issue in WordPress - great job! So, newbie question that I'm hoping you can assist with: in the just-as-awesome enhancement Adding Post Meta Support for Alternate Versions (https://github.com/bu-ist/bu-versions/wiki/Adding-Post-Meta-Support-for-Alternate-Versions) - I've added my meta box to the "page_alt" post type so it displays, but it doesn't actually pull the data, causing me to think that it's me: either I am not registering the feature properly and the data isn't even coming over, OR my meta box is misconfigured to display on the alternate version page and has no clue there is even data to be shown...any help or guidance on proper implementation of this enhancement that you can help us with?

mgburns commented 11 years ago

This is tricky to troubleshoot without seeing some code. In general, two things are required to support custom meta boxes for alternate versions:

  1. Your meta box must be configured (via add_meta_box() args) to display for the desired alternate post types ("page_alt", "post_alt", etc.)
  2. Your meta keys must be registered using the bu_alt_versions_feature_support filter (to clone meta data)

If the meta box is showing up for "Alternate Pages", sounds like you've taken care of 1. If no data is being cloned it sounds like BU Versions doesn't know that it needs to copy your meta keys (2). There is an example of how to use the bu_alt_versions_feature_support filter on the page that you linked to.

A gist would be helpful here.

coeweb commented 11 years ago

Great - I'm 99% there - I know it's more of the big learning curve with meta boxes on my end (the plugin itself is nicely written though!) I am posting two items below, one is my custom meta boxes code and the second is the the post meta support filter with my fields inserted. I have a feeling I'm not adapting the filter properly (especially "feature_name") which I think is causing the problems.

Meta Boxes (as a plugin):

https://gist.github.com/coeweb/15b0ba0da36601c57f1f

And here is the filter adapted:

https://gist.github.com/coeweb/7467fdbeb41b1b68749a

So, is there something totally bonehead that I'm doing wrong or missing?

musicisnotalie commented 11 years ago

This may or may not apply to your situation but the key for me in solving _alt type metaboxes inheriting parent content was including the _alt type with the parent type when registering the metabox. I'm using the fantastic Custom Metaboxes and Fields (https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress) library.

function dol_create_trivia_metaboxes( $meta_boxes ) {

// You'll want to use the same prefix and field names for fields like start and end date,
// or you'll have to rebuild the query sorting as well.
$prefix = 'dol_trivia_';

$meta_boxes[] = array(
'id' => 'dol-trivia-meta',
'title' => 'Trivia Details',
'pages' => array('trivia', 'trivia_alt'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(

array(
    'name'    => 'Question',
    'desc'    => 'Add a trivia question',
    'id'      => $prefix . 'question',
    'type'    => 'textarea_small',
),
array(
    'name'    => 'Answer',
    'desc'    => 'Add the answer to the above trivia question',
    'id'      => $prefix . 'answer',
    'type'    => 'textarea_small',
),
array(
    'name'    => 'Submitted by',
    'desc'    => 'Person responsible for submitting the trivia Q & A',
    'id'      => $prefix . 'submitted_by',
    'type'    => 'text',
),
array(
    'name'    => 'Source',
    'desc'    => 'Where did this trivia question originate?',
    'id'      => $prefix . 'source',
    'type'    => 'text',
), 

) 

);

// When you're done making metaboxes, don't forget to return them.
return $meta_boxes;
}
add_filter( 'cmb_meta_boxes', 'dol_create_trivia_metaboxes' );

And the corresponding BU Versions filter...

//Add custom fields support to BU Versions plugin
add_filter('bu_alt_versions_feature_support', 'dol_register_alt_version_features');
function dol_register_alt_version_features($features) {
    $features['custom-fields'] = array(
        'dol_trivia_question',
        'dol_trivia_answer',
        'dol_trivia_source',
        'dol_trivia_submitted_by'    
    );
    return $features;
}
coeweb commented 11 years ago

Thank you x 10, Superfancy! I felt I wasn't using "feature_name" properly. I thought it was a fill-in to name my feature anything...aargh what a bonehead. Properly renamed it to "custom-fields" (the Wordpress feature) and all is good in the universe.

I've updated my gists to what works for me. BTW, running the filter as a separate plugin so any future BU updates through the WordPress admin panel don't mess with my filter.

Meta Boxes (as a plugin):

https://gist.github.com/coeweb/15b0ba0da36601c57f1f

And here is the filter adapted (as a plugin):

https://gist.github.com/coeweb/7467fdbeb41b1b68749a

musicisnotalie commented 11 years ago

Happy to share! I like the idea of moving these to a functionality plugin, it's something I'd like to do eventually.