Created
March 2, 2016 17:18
-
-
Save miklb/cac47f1dbc10852db29a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chapel_project_metaboxes(array $meta_boxes) | |
{ | |
// underscore hides meta box from custom field list | |
$prefix = '_chapel_'; | |
$meta_boxes ['project_metabox'] = array( | |
'id' => 'project_metabox', | |
'title' => __('Project Overview', 'cmb2'), | |
'object_types' => array( 'project'), | |
'context' => 'normal', | |
'priority' => 'high', | |
'show_names' => true, | |
'fields' => array ( | |
array ( | |
'name' => __('Client' ,'cmb2'), | |
'id' => $prefix . 'project_client', | |
'type' => 'select', | |
'options' => chapel_get_client_options( array ('post_type' => ' clients ', 'numberposts' => -1 , 'orderby' => 'name', 'order' => 'ASC') ) | |
), | |
array ( | |
'name' => __('PO Number', 'cmb2'), | |
'desc' => __('PO Numer for this job.', 'cmb2'), | |
'id' => $prefix . 'projectpo', | |
'type' => 'text_medium' | |
), | |
array ( | |
'name' => __('Project Staus', 'cmb2'), | |
'desc' => __('Status of this project.' ,'cmb2'), | |
'id' => $prefix . 'projectstaus', | |
'type' => 'radio', | |
'options' => array( | |
'estimate' => __('Estimate', 'cmb2'), | |
'invoice' => __('Invoice', 'cmb2'), | |
'completed' => __('Completed', 'cmb2'), | |
) ) , | |
array ( | |
'name' => __('Project Number', 'cmb2'), | |
'desc' => __('Project Numer for this job.', 'cmb2'), | |
'id' => $prefix . 'projectnumber', | |
'type' => 'text_medium' | |
), | |
array ( | |
'name' => __('Due date.', 'cmb2'), | |
'desc' => __('Date project is due.', 'cmb2'), | |
'id' => $prefix . 'projectdue', | |
'type' => 'text_datetime_timestamp', | |
), | |
array ( | |
'name' => __('Date Completed', 'cmb2'), | |
'desc' => __('Date project completed.', 'cmb2'), | |
'id' => $prefix . 'projectcompleted', | |
'type' => 'text_datetime_timestamp', | |
), | |
array ( | |
'name' => __('Invoiced' , 'cmb2'), | |
'desc' => __('Has this poject been invoiced?' ,'cmb2'), | |
'id' => $prefix .'projectinvoiced', | |
'type' =>'checkbox', | |
), | |
) | |
); | |
return $meta_boxes; | |
} | |
add_filter('cmb2_meta_boxes', 'chapel_project_metaboxes'); | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chapel_add_edit_form_to_frontend( $content ) { | |
if ( isset( $_GET['edit'] ) ) { | |
if ( current_user_can( 'edit_posts' ) && wp_verify_nonce( $_GET['edit'], 'edit' ) ) { | |
$content = cmb2_get_metabox_form( '_chapel_project_metabox', get_the_ID() ); | |
} else { | |
$content = '<h2 class="edit-error">You do not have permission to edit this post.</h2>'; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'chapel_add_edit_form_to_frontend' ); | |
/** | |
* Modify the edit links to point to the front-end editor. | |
*/ | |
function chapel_modify_edit_link( $link ) { | |
if ( ! is_admin() ) { | |
$link = esc_url_raw( wp_nonce_url( remove_query_arg( 'edit' ), 'edit', 'edit' ) ); | |
} | |
return $link; | |
} | |
add_filter( 'get_edit_post_link', 'chapel_modify_edit_link' ); | |
/** | |
* Hook in later and prepend our title/content fields to our existing metabox | |
*/ | |
function chapel_edit_core_fields() { | |
if ( ! is_admin() ) { // only if on front-end | |
// Get existing metabox | |
$cmb = cmb2_get_metabox( '_chapel_project_metabox' ); | |
// and prepend title | |
$cmb->add_field( array( | |
'name' => __( 'Title', 'cmb2' ), | |
'id' => 'post_title', | |
'type' => 'text', | |
'before' => 'chapel_edit_core_maybe_redirect', | |
), 1 ); | |
// and content fields | |
$cmb->add_field( array( | |
'name' => __( 'Content', 'cmb2' ), | |
'id' => 'post_content', | |
'type' => 'wysiwyg', | |
), 2 ); | |
} | |
} | |
add_action( 'cmb2_init', 'chapel_edit_core_fields', 99 ); | |
/** | |
* If edit was saved, redirect to non-edit page | |
*/ | |
function chapel_edit_core_maybe_redirect() { | |
if ( isset( $_POST['post_content'] ) ) { | |
$url = esc_url_raw( remove_query_arg( 'edit' ) ); | |
echo "<script type='text/javascript'>window.location.href = '$url';</script>"; | |
} | |
} | |
/** | |
* We don't want CMB2 to fetch data from meta for post title and post content | |
*/ | |
function chapel_cmb2_override_core_field_get( $val, $object_id, $a, $field ) { | |
global $post; | |
if ( in_array( $field->id(), array( 'post_title', 'post_content' ), true ) ) { | |
if ( isset( $post->ID ) ) { | |
$val = get_post_field( $field->id(), $post ); | |
} else { | |
$val = ''; | |
} | |
} | |
return $val; | |
} | |
add_filter( 'cmb2_override_meta_value', 'chapel_cmb2_override_core_field_get', 10, 4 ); | |
/** | |
* We don't want CMB2 to save data to meta for post title and post content | |
*/ | |
function chapel_cmb2_override_core_field_set( $status, $a, $args, $field ) { | |
global $post; | |
if ( in_array( $field->id(), array( 'post_title', 'post_content' ), true ) ) { | |
if ( isset( $post->ID ) ) { | |
$status = wp_update_post( array( | |
$field->id() => $a['value'], | |
'ID' => $post->ID, | |
) ); | |
} else { | |
$status = false; | |
} | |
} | |
return $status; | |
} | |
add_filter( 'cmb2_override_meta_save', 'chapel_cmb2_override_core_field_set', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest in your field/mb registration that you follow the example in the example-functions.php file: https://github.com/WebDevStudios/CMB2/blob/master/example-functions.php#L68-L360
Also, the cmb ID you call here and here needs to be the same cmb id as here.