Skip to content

Instantly share code, notes, and snippets.

@anthonybudd
Last active April 4, 2018 18:42
Show Gist options
  • Save anthonybudd/80a3d3233c1dde86e2a0f5bc4402428c to your computer and use it in GitHub Desktop.
Save anthonybudd/80a3d3233c1dde86e2a0f5bc4402428c to your computer and use it in GitHub Desktop.
public static function fieldsCallback(){
$this_ = Self::newWithoutConstructor();
if(method_exists($this_, 'cmb2Fields')){
$fields = $this_->cmb2Fields();
if(function_exists('new_cmb2_box') && is_array($fields)){
if(!isset($fields['meta_box'])){
$fields['meta_box'] = [
'id' => get_called_class(),
'title' => get_called_class(),
'object_types' => array($this_->postType),
];
}
$cmb = new_cmb2_box($fields['meta_box']);
unset($fields['meta_box']);
foreach($fields as $field){
$cmb->add_field($field);
}
}
}
}
public function cmb2Fields(){
return [
[
'name' => 'Test Text',
'id' => 'text',
'type' => 'text',
],[
'name' => 'Website URL', 'cmb2',
'id' => 'url',
'type' => 'text_url',
]
];
}
add_action('cmb2_admin_init', [get_called_class(), 'fieldsCallback']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment