Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render Form Fields separately / dynamically on the front end #506

Closed
ardiansyaherwin opened this issue Oct 27, 2015 · 1 comment
Closed

Comments

@ardiansyaherwin
Copy link

Hi,

Is there any hooks / function to render the form fields individually?

Because when you use cmb2_get_metabox_form, it automatically added the form and the fields in a single call based on cmb2_metabox_form(). I mean is there any function to call like,

$the_forms = cmb2_get_metabox_form($meta_box, $object_id, '');
foreach $the_forms as $the_form => $key{
    echo $key->form_header(); // the opening form tag along with the nonce and hidden fields
    echo $key->form_field('field_id_1');
    echo $key->form_field('field_id_2');
    echo "------"; // this could be anything like the opening/closing div tag
    echo $key->form_field('field_id_3');
    echo $key->form_field('submit');
    echo $key->form_footer(); // the closing form tag
}

You know, it's for the front end templating matters.

Thanks.

@ardiansyaherwin
Copy link
Author

@jtsternberg hi big thanks for the commits, and I'm sorry for not very clear on my questions.

So basically, I've achieved what I wanted to, but with a little copy this and that from the core.
Here's my page template:

<?php
$GLOBALS['getfront_cmb']  = wds_frontend_cmb2_get(); // get the object array
CMB2_hookup::enqueue_cmb_css(); //for the wyswyg, I have to do this manually
CMB2_hookup::enqueue_cmb_js(); //for the wyswyg, I have to do this manually

function getcmb_atts($id, $atts){ // to get only a specific attribute
    $that_field = $GLOBALS['getfront_cmb']->meta_box['fields'][$id][$atts];
    return $that_field;
  }
  function getcmb_field($id){ // THIS is the RENDER method, that I copied from the plugin
    $that_field = $GLOBALS['getfront_cmb']->meta_box['fields'][$id];
    $field = $GLOBALS['getfront_cmb']->get_field( $that_field )->render_field();
    return $field;
  }
?>
<!-- the form -->
<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 ) ?>

<!-- .... whatever bootstrap or foundation or those grids html tags ... -->
<?php getcmb_field("submitted_post_content"); //this is for the wyswyg field ?>
<!-- .... whatever bootstrap or foundation or those grids html tags ... -->
<!-- .... whatever bootstrap or foundation or those grids html tags ... -->
<?php getcmb_field("submitted_post_title"); // text field ?>
<!-- .... whatever bootstrap or foundation or those grids html tags ... -->

<!-- this is the magic of the first function, let's say we want to render a text field (you have to acknowledge the rendered field should be look like first) -->
<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'); ?>">

<!-- finally the closing form -->
<input type="hidden" name="atts[post_author]" id="atts[post_author]" value="<?php echo get_current_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>

</form>

Is my code above a bad practice or a pretty good one?
Please let me know, I'm afraid it will break the template once there's CMB2 updates in the future.

Hope this helps someone!
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant