• Resolved grohlbainselic

    (@grohlbainselic)


    I need some help on CMB2 plugin for WordPress. Would you?

    I have a QuForm and I’m using its fields values to populate a new post, via this function:

            add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
                $title = $form->getValue('quform_1_3');
                $content = $form->getValue('quform_1_9');
    
                $post = array(
                    'post_title' => $title,
                    'post_content' => $content,
                    'post_type' => 'ficha',
            'post_status' => 'publish',
            );
    
                wp_insert_post($post);
             
                return $result;
            }, 10, 2);

    quform_1_3 refers to “Name” field on my form. quform_1_9 refers to “E-mail” field.

    When someone, on my frontend, fills that form, a new post is generated with those info inserted on post_title and post_content. That post is marked as ‘ficha’ CTP. Post status is setted as PUBLISHED.

    Now, my doubt.

    In this CTP, named as “ficha”, I have a CMB2 custom metabox with 2 fields: “name” and “e-mail”.

    image.png

    On my function, how can I use those quform form fielsd (quform_1_3 and quform_1_9) to insert their values (filled by someone on my frontend) in those metaboxes fields? Is this possible?

    Something like getting post meta and inserting quform_1_3 variable in it… I’m breaking my head with this like 3 days long.

    Can you help me? I would be so thankful.

    Regards,
    Vyctor

    ——————————————-

    My metabox creation:

    ` add_action( ‘cmb2_admin_init’, ‘cmb2_sample_metaboxes’ );
    /**
    * Define the metabox and field configurations.
    */
    function cmb2_sample_metaboxes() {

    /**
    * Initiate the metabox
    */
    $cmb = new_cmb2_box( array(
    ‘id’ => ‘qu_form’,
    ‘title’ => __( ‘Dados da Ficha’, ‘cmb2’ ),
    ‘object_types’ => array( ‘ficha’, ), // Post type
    ‘context’ => ‘normal’,
    ‘priority’ => ‘high’,
    ‘show_names’ => true, // Show field names on the left
    // ‘cmb_styles’ => false, // false to disable the CMB stylesheet
    // ‘closed’ => true, // Keep the metabox closed by default
    ) );

    // Regular text field
    $cmb->add_field( array(
    ‘name’ => __( ‘Nome’, ‘cmb2’ ),
    ‘desc’ => __( ‘Aqui vai o nome’, ‘cmb2’ ),
    ‘id’ => ‘ficha_nome’,
    ‘type’ => ‘text’,
    ‘show_on_cb’ => ‘cmb2_hide_if_no_cats’, // function should return a bool value
    // ‘sanitization_cb’ => ‘my_custom_sanitization’, // custom sanitization callback parameter
    // ‘escape_cb’ => ‘my_custom_escaping’, // custom escaping callback parameter
    // ‘on_front’ => false, // Optionally designate a field to wp-admin only
    // ‘repeatable’ => true,
    ) );

    // Email text field
    $cmb->add_field( array(
    ‘name’ => __( ‘E-mail’, ‘cmb2’ ),
    ‘desc’ => __( ‘Aqui vai o e-mail’, ‘cmb2’ ),
    ‘id’ => ‘ficha_email’,
    ‘type’ => ‘text_email’,
    // ‘repeatable’ => true,
    ) );

    // Add other metaboxes as needed

    }

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

    (@tw2113)

    The BenchPresser

    The wp_insert_post function returns the post ID of the newly created post it makes. If that same post is the one you’re needing to update the metadata for, just use that returned ID to update your appropriate fields before doing return $result

    Thread Starter grohlbainselic

    (@grohlbainselic)

    Hi, Michael.

    Would it be something like this?

    
    add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
        $title = $form->getValue('quform_1_3');
        $content = $form->getValue('quform_1_9');
    	$nomeainserir = $form->getValue('quform_1_6');
    	
        $post = array(
            'post_title' => $title,
            'post_content' => $content,
            'post_type' => 'ficha',
    		'post_status' => 'draft',
    
    		);
        $post_id = wp_insert_post( $post ); 
    	
    	update_post_meta( $post_id, 'ficha_nome', $nomeainserir );
    	update_post_meta( $post_id, 'ficha_email', $nomeainserir );
    	update_post_meta( $post_id, 'post_status', 'publish' );
     
        return $result;
    }, 10, 2);
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    almost. Need to assign the $post_id variable. Like so:

    $post_id = wp_insert_post($post);
    
    Thread Starter grohlbainselic

    (@grohlbainselic)

    Thanks! It worked!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use form field inputs to inject field value on CMB2 metaboxes’ is closed to new replies.