Skip to content

Instantly share code, notes, and snippets.

@tharsheblows
Last active March 10, 2019 22:27
Show Gist options
  • Save tharsheblows/6ea6f0dda3db8df57563d2d169e4dbb7 to your computer and use it in GitHub Desktop.
Save tharsheblows/6ea6f0dda3db8df57563d2d169e4dbb7 to your computer and use it in GitHub Desktop.
using custom tables with CMB2
<?php
// I've copied and pasted then edited this without testing, apols for any errors. It's for illustrative purposes mainly :
// https://tharshetests.wordpress.com/2017/12/27/using-custom-tables-with-cmb2/
// The idea here is that I want to "methods" metaboxes which are going to be a repeatable group with the method itself
// and whichever countries the metabox applies to.
// So eg for a recipe, you'd have "cook the aubergine" in the UK and "cook the eggplant" in the US.
// The methods are stored in custom tables (this is pretend, the code below doesn't do that)
// Adding the metaboxes happens as normal
add_action( 'cmb2_admin_init', 'mjj_add_methods_metaboxes' );
function mjj_add_methods_metaboxes(){
// as normal
$metabox = new_cmb2_box( array(
'id' => 'mjj_mthd_mb',
'title' => __( 'Methods', 'mjj' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true
) );
// as normal
$method_group = $metabox->add_field( array(
'id' => '_mjj_methods',
'type' => 'group',
'description' => __( 'Different countries have different words for things, so you might need more than one method', 'mjj' ),
'options' => array(
'group_title' => __( 'Method {#}', 'mjj' ),
'add_button' => __( 'Add Another Method', 'mjj' ),
'remove_button' => __( 'Remove Method', 'mjj' ),
),
) );
// as normal
$metabox->add_group_field( $method_group, array(
'name' => 'Method',
'id' => 'method',
'type' => 'textarea',
) );
// as normal
$metabox->add_group_field( $method_group, array(
'name' => 'Countries',
'desc' => 'In which countries should this method be shown? (Only US, UK and AUS for now)',
'id' => 'countries',
'type' => 'multicheck',
'options' => array(
'US' => 'US',
'UK' => 'UK',
'AUS' => 'AUS',
),
) );
}
// This bit is to get the values and put them in the metaboxes on the post edit page.
// I could also hook into cmb2_override_meta_value and look for the _mjj_methods field id and only run this on that.
add_filter('cmb2_override__mjj_methods_meta_value', 'mjj_get_methods_custom_data', 10, 4);
function mjj_get_methods_custom_data( $dont_override, $object_id, $args, $cmb2_field_object ){
// Below is getting my data, you can't really copy and paste this and have it work.
// $mjj_methods = new MJJ_Methods;
// $methods = $mjj_methods->get( array( 'object_id' => $object_id ) );
// Ok, let's pretend that $methods got this:
$methods = array(
array(
'method' => 'This is a method for the US and AUS'
'countries' => array( 'US', 'AUS' )
),
array(
'method' => 'This is a method for the UK'
'countries' => array( 'UK' )
)
);
return $methods;
}
// This saves the metaboxes.
// runs when the _mjj_methods goes to get updated / saved
add_filter('cmb2_override__mjj_methods_meta_save', 'mjj_save_methods_custom_data', 10, 4 );
function mjj_save_methods_custom_data( $check, $args, $field_args, $cmb2_field_object ){
// each argument is a superset of the argument before :)
// do all the custom saving bits here
return 'something other than null'; // return something other than null
}
// This is a useful field to have on hand.
// add a hidden field in which to put the id -- I can use this with 'type' => 'mjj_hidden_field' and it works in repeatable groups
add_action( 'cmb2_render_mjj_hidden_field', 'mjj_hidden_field', 10, 5 );
function mjj_hidden_field( $field, $escaped_value, $object_id, $object_type, $field_type_object ){
echo $field_type_object->input( array( 'type' => 'hidden' ) );
}
@dskurth
Copy link

dskurth commented Mar 10, 2019

Nice. Thank you for this. Tell me, do you know why the call back function gets called for the total number of fields plus 1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment