Skip to content

Instantly share code, notes, and snippets.

@jdcauley
Created December 5, 2016 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdcauley/265d162d438c24c6f6e823e632a55c47 to your computer and use it in GitHub Desktop.
Save jdcauley/265d162d438c24c6f6e823e632a55c47 to your computer and use it in GitHub Desktop.
Shared fields
function sponsor_fields(){
$sponsor_id = '';
if( isset( $_GET['post'] ) ){
$sponsor_id = get_post_meta( $_GET['post'], '_os_sponsor_id', true );
} else if( isset( $_GET['taxonomy'] ) ){
$sponsor_id = get_term_meta( $_GET['tag_ID'], '_os_sponsor_id', true );
} else {
return;
}
$prefix = '_os_sponsor_';
$post_types = get_post_types();
unset($post_types['nav_menu_item']);
unset($post_types['attachment']);
unset($post_types['revision']);
$sponsored_post_types = array();
foreach($post_types as $key => $value){
$sponsored_post_types[] = $value;
}
$sponsor_id_field = array(
'name' => 'Select Sponsor',
'id' => $prefix . 'id',
'type' => 'select',
'show_option_none' => true,
'options' => array(),
'attributes' => array(
'data-value' => $sponsor_id
)
);
$sponsor_link = array(
'name' => __( 'Sponsor Link', 'cmb2' ),
'id' => $prefix . 'sponsor_link',
'type' => 'text_url'
);
$post_types_cmb = new_cmb2_box( array(
'id' => 'os_sponsor_meta',
'title' => __( 'OS Sponsor Data', 'cmb2' ),
'object_types' => $sponsored_post_types, // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true
) );
$post_types_cmb->add_field( $sponsor_id_field );
$post_types_cmb->add_field( $sponsor_link );
$taxonomy_cmb = new_cmb2_box( array(
'id' => 'os_sponsor_tax_meta',
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'series' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true
) );
$taxonomy_cmb->add_field( $sponsor_id_field );
$taxonomy_cmb->add_field( $sponsor_link );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment