Skip to content

Instantly share code, notes, and snippets.

@ardiansyaherwin
Created October 28, 2015 17:25
Show Gist options
  • Save ardiansyaherwin/2bfffaa7a61e34ed2e59 to your computer and use it in GitHub Desktop.
Save ardiansyaherwin/2bfffaa7a61e34ed2e59 to your computer and use it in GitHub Desktop.
Wordpress page template for CMB2 custom rendering the fields dynamically or separately
<?php
// Get CMB2 metabox object
$GLOBALS['getfront_cmb'] = wds_frontend_cmb2_get();
// Get $GLOBALS['getfront_cmb'] object_types
$post_types = $GLOBALS['getfront_cmb']->prop( 'object_types' );
// Current user
$user_id = get_current_user_id();
// Parse attributes
$atts = shortcode_atts( array(
'post_author' => $user_id ? $user_id : 1, // Current user, or admin
'post_status' => 'pending',
'post_type' => reset( $post_types ), // Only use first object_type in array
), $atts, 'cmb-frontend-form' );
/*
* Let's add these attributes as hidden fields to our cmb form
* so that they will be passed through to our form submission
*/
foreach ( $atts as $key => $value ) {
$GLOBALS['getfront_cmb']->add_hidden_field( array(
'field_args' => array(
'id' => "atts[$key]",
'type' => 'hidden',
'default' => $value,
),
) );
}
// Initiate our output variable
$output = '';
$action_msg = '';
// Get any submission errors
if ( ( $error = $GLOBALS['getfront_cmb']->prop( 'submission_error' ) ) && is_wp_error( $error ) ) {
// If there was an error with the submission, add it to our ouput.
$action_msg .= '<h3>' . sprintf( __( 'There was an error in the submission: %s', 'wds-post-submit' ), '<strong>'. $error->get_error_message() .'</strong>' ) . '</h3>';
}
// If the post was submitted successfully, notify the user.
if ( isset( $_GET['post_submitted'] ) && ( $post = get_post( absint( $_GET['post_submitted'] ) ) ) ) {
// Get submitter's name
$name = get_post_meta( $post->ID, 'submitted_author_name', 1 );
$name = $name ? ' '. $name : '';
// Add notice of submission to our output
$action_msg .= '<h3>' . sprintf( __( 'Thank you%s, your new post has been submitted and is pending review by a site administrator.', 'wds-post-submit' ), esc_html( $name ) ) . '</h3>';
}
// Get our form
$output .= cmb2_get_metabox_form( $GLOBALS['getfront_cmb'], 'fake-oject-id', array( 'save_button' => __( 'Submit Post', 'wds-post-submit' ) ) );
?>
<?php
$user_ID = get_current_user_id();
$user_nickname = get_the_author_meta( "nickname", $user_ID );
$user_avatar = get_avatar_url( $user_ID );
$user_post_count = count_user_posts( $user_ID );
?>
<?php
CMB2_hookup::enqueue_cmb_css();
CMB2_hookup::enqueue_cmb_js();
//var_dump($GLOBALS['getfront_cmb']->meta_box['fields']['submitted_post_content']);
function getcmb_name($id, $atts){
$that_field = $GLOBALS['getfront_cmb']->meta_box['fields'][$id][$atts];
return $that_field;
}
function getcmb_field($id){
$that_field = $GLOBALS['getfront_cmb']->meta_box['fields'][$id];
$field = $GLOBALS['getfront_cmb']->get_field( $that_field )->render_field();
return $field;
}
?>
<form class="cmb-form post-form" method="post" id="<?php echo $GLOBALS['getfront_cmb']->cmb_id; ?>" enctype="multipart/form-data" encoding="multipart/form-data">
<input type="hidden" name="object_id" value="<?php echo $GLOBALS['getfront_cmb']->object_id; ?>">
<?php wp_nonce_field( $GLOBALS['getfront_cmb']->nonce(), $GLOBALS['getfront_cmb']->nonce(), false, true ) ?>
<div class="row">
<div class="col-xs-12 col-sm-4">
<nav class="navbar navbar-default navbar-side">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#sidenav" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="sidenav-header">
<img src="<?php echo esc_url( $user_avatar ); ?>" alt="<?php echo $user_nickname; ?>" class="full-rounded" />
<div class="user-summary">
<strong><?php echo $user_nickname; ?></strong>
<a href="published-article.html" title="view published article"><?php echo $user_post_count; ?> <?php echo $user_post_count > 1 ? "articles":"article"; ?> published</a>
</div>
</div>
</div>
<div id="sidenav" class="navbar-collapse collapse">
<ul class="nav nav-pills nav-stacked dash-nav collapse-list-container">
<li>
<a href="#" id="trigger_set-featured-image_modal" data-toggle="modal" data-target="#set-featured-image_modal">
<i class="icon-add-image"></i>
Set Featured Image
</a>
<div class="modal small-modal" id="set-featured-image_modal" tabindex="-1" role="dialog" aria-labelledby="set-featured-image_modalLabel" data-color="transparent">
<div class="vertical-alignment-helper">
<div class="modal-dialog vertical-align-center" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Set Featured Image</h4>
</div>
<div class="modal-body">
<p>Drag and drop here or click to upload</p>
<i class="icon-add-image"></i>
<em>Allowed file types: jpg, jpeg, png, gif</em>
<span>Maximum upload file size: 10 MB.</span>
<?php getcmb_field("submitted_post_thumbnail"); ?>
</div>
<div class="modal-footer">
<button type="button" class="hr-btn medium-grey">Done</button>
</div>
<div style="clear:both;"></div>
</div>
</div>
</div> <!-- .vertical-alignment-helper -->
</div>
</li>
<li>
<a title="Highlighted Quote" role="button" data-toggle="collapse" href="#collapse_quote" aria-expanded="false" aria-controls="collapse_quote">
<i class="icon-add-quote"></i>
Highlighted Quote
<span class="caret"></span>
</a>
<div class="collapse" id="collapse_quote">
<div class="input-container">
<textarea name="highlighted-qote"></textarea>
</div>
</div>
</li>
<li>
<a title="Categories" role="button" data-toggle="collapse" href="#collapse_categories" aria-expanded="false" aria-controls="collapse_categories">
<i class="icon-folder"></i>
Categories
<span class="caret"></span>
</a>
<div class="collapse" id="collapse_categories">
<div class="input-container">
<input type="text" name="post-categories" />
</div>
</div>
</li>
<li>
<a title="Tags" role="button" data-toggle="collapse" href="#collapse_tags" aria-expanded="false" aria-controls="collapse_tags">
<i class="icon-tags"></i>
Tags
<span class="caret"></span>
</a>
<div class="collapse" id="collapse_tags">
<div class="input-container">
<input type="text" name="post-tags" />
</div>
</div>
</li>
<li>
<a title="Photo Credit" role="button" data-toggle="collapse" href="#collapse_photo-credit" aria-expanded="false" aria-controls="collapse_photo-credit">
<i class="icon-credit"></i>
Photo Credit
<span class="caret"></span>
</a>
<div class="collapse" id="collapse_photo-credit">
<div class="input-container">
<input type="text" name="post-photo-credit" />
</div>
</div>
</li>
</ul>
</div><!--/.nav-collapse -->
</nav>
</div>
<div class="col-xs-12 col-sm-8">
<div class="dashboard-content form-container">
<h3><?php echo __('Create New Post', 'scribblink'); ?></h3>
<div class="article-content-editor_container">
<div class="input-container">
<input type="text" class="regular-text italic-placeholder" name="submitted_post_title" id="submitted_post_title" placeholder="<?php echo getcmb_atts('submitted_post_title', 'placeholder'); ?>">
</div>
<div class="input-container">
<input type="text" class="regular-text italic-placeholder" name="submitted_sub_post_title" id="submitted_sub_post_title" placeholder="<?php echo __( 'Type a subtitle', 'scribblink' ); ?>">
</div>
<div class="input-container">
<div class="media-control">
<a href="#" title="insert image" class="add-image">
<i class="icon-add-image"></i>
</a>
<a href="#" title="insert media" class="add-media">
<i class="icon-add-media"></i>
</a>
<a href="#" title="insert URL" class="add-url">
<i class="icon-add-url"></i>
</a>
</div>
</div>
<div class="textarea-count-container">
<?php getcmb_field("submitted_post_content"); ?>
</div>
</div>
<div class="input-container button-container">
<a href="#" class="trash-post" title="trash it">
<i class="icon-trash"></i>Trash
</a>
<input type="hidden" name="atts[post_author]" id="atts[post_author]" value="<?php echo $user_ID; ?>">
<input type="hidden" name="atts[post_status]" id="atts[post_status]" value="pending">
<input type="hidden" name="atts[post_type]" id="atts[post_type]" value="post">
<button type="submit" name="submit-cmb" id="submit-cmb" title="Publish" class="hr-btn light-grey">Publish</button>
<a href="#" title="Save Draft">Save Draft</a>
<a href="#" title="Preview">Preview</a>
</div>
</div>
</div>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment