Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If it’s a field that’s able to be repeatable, then it’ll be true regardless of where the metabox contents are output. You are also able to have the user metaboxes output on the frontend and update user meta data from the frontend, it just may take some tinkering and reviewing of examples to get it working perfectly. We don’t always have code snippets available for every use case.

    Thread Starter andp

    (@andp)

    Hello Michael and thanks for your quick reply on my previous question.

    So far i managed to create the desired custom user fields (repeatable, rebeatable boxes) on the back-end and they are working OK.

    I also found a useful post about how to use cmb2 fields in the front-end (https://webdevstudios.com/2015/03/30/use-cmb2-to-create-a-new-post-submission-form/).

    I am struggling though of how to use my custom user fields on the front-end. My purpose is not only to display them but to make them editable.

    I would appreciate any kind of help or hint on this.

    Thank u

    Plugin Author Justin Sternberg

    (@jtsternberg)

    This snippet is for displaying/editing fields on the front-end. It works by adding a shortcode, but hopefully you can see how it works. You would just substitute the post id with the user id.

    Thread Starter andp

    (@andp)

    hello again

    i am using your given examples but i cant get it right.

    add_action( ‘cmb2_init’, ‘yourprefix_register_user_profile_metabox’ );
    function yourprefix_register_user_profile_metabox() {

    // Start with an underscore to hide fields from custom fields list
    $prefix = ‘_yourprefix_user_’;

    /**
    * Metabox for the user profile screen
    */
    $cmb_user = new_cmb2_box( array(
    ‘id’ => $prefix . ‘edit’,
    ‘title’ => __( ‘User Profile Metabox’, ‘cmb2’ ),
    ‘object_types’ => array( ‘user’ ), // Tells CMB to use user_meta vs post_meta
    ‘show_names’ => true,
    ‘new_user_section’ => ‘add-new-user’, // where form will show on new user page. ‘add-existing-user’ is only other valid option.
    ) );

    $cmb_user->add_field( array(
    ‘name’ => __( ‘Extra Info’, ‘cmb2’ ),
    ‘desc’ => __( ‘field description (optional)’, ‘cmb2’ ),
    ‘id’ => $prefix . ‘extra_info’,
    ‘type’ => ‘title’,
    ‘on_front’ => false,
    ) );

    $cmb_user->add_field( array(
    ‘name’ => __( ‘Something’, ‘cmb2’ ),
    ‘desc’ => __( ‘field description (optional)’, ‘cmb2’ ),
    ‘id’ => $prefix . ‘something’,
    ‘type’ => ‘textarea’,
    ) );}

    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[‘user_id’] ) ) {
    $atts[‘user_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[‘user_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’ );

    /*shortcode [cmb-form id=”edit” user_id=1]*/

    Thread Starter andp

    (@andp)

    Hello i figured it out

    thank again for your help

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Awesome to hear, andp.

    Hi andp
    Is there any chance that you could post what you figured out?
    Many thanks
    Onebob

    Same here, I would appreciate it ??

    Thanks!

    Got it!

    Just need to correct the $post->ID by $user->ID and also add your prefix to the shortcode for the id attribute, like:

    [cmb-form id=”_yourprefix_user_edit” user_id=1]

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘user profile – front end’ is closed to new replies.