Are you sure this line is providing a value, and the right one?
$metabox_id = esc_attr( $atts['id'] );
Whats your metabox configuration code as well?
<?php
/*
* Plugin Name: CMB2 - Front-end Shortcode
* Description: Display CMB2 metabox forms on the front-end using a shortcode
* Author: jtsternberg
* Author URI: http://dsgnwrks.pro
* Version: 0.1.0
*/
/**
* Shortcode to display a CMB2 form for a post ID.
* Adding this shortcode to your WordPress editor would look something like this:
*
* [cmb-form id="test_metabox" post_id=2]
*
* The shortcode requires a metabox ID, and (optionally) can take
* a WordPress post ID (or user/comment ID) to be editing.
*
* @param array $atts Shortcode attributes
* @return string Form HTML markup
*/
function jt_cmb2_do_frontend_form_shortcode( $atts = array() ) {
global $post;
/**
* Make sure a WordPress post ID is set.
* We'll default to the current post/page
*/
if ( ! isset( $atts['post_id'] ) ) {
$atts['post_id'] = $post->ID;
}
// If no metabox id is set, yell about it
if ( empty( $atts['id'] ) ) {
return 'Please add an "id" attribute to specify the CMB2 form to display.';
}
$metabox_id = esc_attr( $atts['id'] );
$object_id = absint( $atts['post_id'] );
// echo(cmb2_get_metabox_form($metabox_id));
// Get our form
$form = cmb2_get_metabox_form( $metabox_id, $object_id );
return $form;
}
add_shortcode( 'cmb-form', 'jt_cmb2_do_frontend_form_shortcode' );
just like the example
and then at the page.php:
<?php echo do_shortcode(' [cmb-form id="_startup_idea"]' ); ?>
What about the config you’re using, like highlighted at https://github.com/WebDevStudios/CMB2/blob/master/example-functions.php#L112-L140 ? That’s what I originally meant for configuration. What sets up the fields and whatnot to be displayed.
add_action( 'cmb2_init', 'cmb2_add_geninfo' );
function cmb2_add_geninfo() {
$prefix = '_startup_';
$info = new_cmb2_box( array(
'id' => $prefix . 'presentation',
'title' => __( 'Presentation', 'cmb2' ),
'object_types' => array( 'startup' ),
'context' => 'normal',
) );
$idea = $info->add_field( array(
'name' => __( 'Idea', 'cmb2' ),
'id' => $prefix . 'idea',
'on_front' => true, // Optionally designate a field to wp-admin only
'type' => 'wysiwyg',
'options' => array(
'textarea_rows' => 20,
'media_buttons' => true,
),
) );
$model = $info->add_field( array(
'name' => __( 'Business Model', 'cmb2' ),
'id' => $prefix . 'business_model',
'type' => 'wysiwyg',
'options' => array(
'textarea_rows' => 50,
'media_buttons' => true,
),
) );
}
the fields setup is fine
From what I can tell, with some testing to confirm, is that you’re passing the wrong string into the shortcode.
<?php echo do_shortcode(' [cmb-form id="_startup_idea"]' ); ?>
That’s the ID for one of the fields in the metabox you’re setting up. However, what you want to pass in is the ID from the new_cmb2_box() line.
<?php echo do_shortcode(' [cmb-form id="_startup_presentation"]' ); ?>
That’s the proper ID for the entire metabox created, and all its fields.
Also, if you’re not using the shortcode, you should just call the function directly, passing it the args (vs calling do_shortcode()
). e.g.:
<?php echo jt_cmb2_do_frontend_form_shortcode( array( 'id' => '_startup_presentation' ) ); ?>
Thanx for the quick response!
guess the readme file was a bit confusing on this…
every thing works fine!