Support » Plugin: CMB2 » Front end submission to custom post type with custom metaboxes

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    The functions.php for your active theme or a custom plugin would both be valid places for those code snippets. So mostly up to your preference for where they go.

    Thread Starter newbars

    (@newbars)

    Nice, thanks for the answer i will try that example

    i got another question for you, in one of the metaboxes i had a radio field

    if i want to make a custom form not using that snippet how can i show the radio options in that form so users can select and save that option in that field?

    Thanks again!

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not quite following you here.

    Are you trying to make use of one CMB2 configuration, but use it in both front and back end? And on the front end not have it have a specific field?

    Or are you trying to add a radio button separate from the CMB2 configuration?

    Thread Starter newbars

    (@newbars)

    I try to explain better, English isn’t my first language so sorry for the mistakes

    I have a custom post type called business and created a metabox for that CPT the idea of this CPT is that my logged users can post their companies

    I want to create a front end form to my users submit the information and get save in the metabox I create

    One of the fields is a radio button called employee numbers

    I want that the options of that radio button display on the form so the users can select one and later save in that custom field

    Is this possible? If is possible how we can do that or I have to use that snippet?

    • This reply was modified 3 months, 1 week ago by newbars.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I don’t see why you couldn’t handle this.

    https://github.com/CMB2/CMB2/wiki/Field-Types#radio

    The radio button field type is readily available.

    'options'          => array(
    	'standard' => __( 'Option One', 'cmb2' ),
    	'custom'   => __( 'Option Two', 'cmb2' ),
    	'none'     => __( 'Option Three', 'cmb2' ),
    ),
    

    Only potential “gotcha” that I can think of would be making sure the parts on the left hand side of this example array are matching up with the employee number values that you’ve already been saving. For example if you have them saved as “10-15” in the meta already, you’d want this or similar:

    '10-15' => __( '10 to 15 people', 'cmb2' ),
    

    and so on with the other options you’re wanting to have available.

    Thread Starter newbars

    (@newbars)

    So i just make a for each in the select to display my options create with

    'options'          => array(
    	'standard' => __( 'Option One', 'cmb2' ),
    	'custom'   => __( 'Option Two', 'cmb2' ),
    	'none'     => __( 'Option Three', 'cmb2' ),
    ),

    And the using the update_post_meta fuctions i can save up the value

    For example these are my options in the radio field

    $empresas_metaboxes->add_field( array(
            'name'    => esc_html__( 'No. de empleados', 'studiare' ),
            'id'      => 'empleados_empresa',
            'type'    => 'radio',
            'options' => array(
                '2-5' => '2-5 empleados',
                '5-10' => '5-10 empleados',
                '10-15' => '10-15 empleados',
                '20' => '+20 empleados',
            ),
        ) );

    and the select would be

    <select id="mytheme_custom_select" name="mytheme_custom_select">
        <option value="2-5">2-5 empleados</option>
        <option value="5-10">5-10 empleados.</option>
        <option value="10-15">10-15 empleados</option>
        <option value="20">20 empleados</option>
    </select>

    And in the query for save post just run update_post_meta($post_id, '_studiare_empleados_empresa', sanitize_text_field($_POST['mytheme_custom_select']));

    • This reply was modified 3 months, 1 week ago by newbars.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Shouldn’t need to worry about the usage of update_post_meta() and whatnot, that should already be getting handled for you with CMB2. Just need to set up the add_field( ... ) call like in your example above.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.