-
-
Save jtsternberg/b90080e5896fb3b88a54 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
<?php | |
// Do this so that the chapel_get_client_options WP_Query/get_posts ONLY happens | |
// when the field is run (otherwise this query will run on EVERY wordpress page-load, | |
// whether page has CMB2 fields or not) | |
public function chapel_get_client_options_cb() { | |
return chapel_get_client_options( array( 'post_type' => ' clients ', 'numberposts' => -1 , 'orderby' => 'name', 'order' => 'ASC' ) ); | |
} | |
function chapel_project_metaboxes() { | |
// underscore hides meta box from custom field list | |
$prefix = '_chapel_'; | |
// YOUR_TEXTDOMAIN == This should be something unique to this build.. like 'chapel' | |
$cmb = new_cmb2_box( array( | |
'id' => 'project_metabox', | |
'title' => __( 'Project Overview', 'YOUR_TEXTDOMAIN' ), | |
'object_types' => array( 'project' ), | |
) ): | |
$cmb->add_field( array( | |
'name' => __( 'Client' ,'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'project_client', | |
'type' => 'select', | |
// Do this so that the chapel_get_client_options WP_Query/get_posts ONLY happens | |
// when the field is run (otherwise this query will run on EVERY wordpress page-load, | |
// whether page has CMB2 fields or not) | |
'options_cb' => 'chapel_get_client_options_cb', | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'PO Number', 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'PO Numer for this job.', 'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'projectpo', | |
'type' => 'text_medium' | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'Project Staus', 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'Status of this project.' ,'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'projectstaus', | |
'type' => 'radio', | |
'options' => array( | |
'estimate' => __( 'Estimate', 'YOUR_TEXTDOMAIN' ), | |
'invoice' => __( 'Invoice', 'YOUR_TEXTDOMAIN' ), | |
'completed' => __( 'Completed', 'YOUR_TEXTDOMAIN' ), | |
) | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'Project Number', 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'Project Numer for this job.', 'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'projectnumber', | |
'type' => 'text_medium' | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'Due date.', 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'Date project is due.', 'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'projectdue', | |
'type' => 'text_datetime_timestamp', | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'Date Completed', 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'Date project completed.', 'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix . 'projectcompleted', | |
'type' => 'text_datetime_timestamp', | |
) ); | |
$cmb->add_field( array( | |
'name' => __( 'Invoiced' , 'YOUR_TEXTDOMAIN' ), | |
'desc' => __( 'Has this poject been invoiced?' ,'YOUR_TEXTDOMAIN' ), | |
'id' => $prefix .'projectinvoiced', | |
'type' =>'checkbox', | |
) ); | |
} | |
add_filter( 'cmb2_init', '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
<?php | |
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( '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( '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