Custom Fields
Re-Envisioned
Support General Select a Gravity Form to assign to a CPT

  • Creator
    Topic
  • #13226
    Resolved Mark Tenney
    Participant

    I have a CPT that I’d like to be able to add a custom field to for a Gravity Form. I tried using the Post field type, but Gravity Forms don’t show up as a CPT. Is there some code I can add into a functions.php to make Gravity Forms available in the post field? Or is there a way to leverage MB relationships? Not sure what the best course of action is. Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Author
    Replies
  • #13231
    Mark Tenney
    Participant

    Reading back over this post, I realize I might need to explain it better. Here’s what I want to accomplish:

    I have a post type called ‘connect-card’ that is geared toward quick interactions with users for churches. I have a page that displays the post listing in a card format here:

    https://yourdomain.church

    So if a user wants to respond to any of these calls to action, they would tap into the ‘connect-card’ and then respond. I have three types of responses: Button, Form, and Redirect.

    The redirect card simply redirects to another page on the site or another external link.

    The button is similar to the redirect, but would present some information before they click on the button to respond.

    The form would be a Gravity Form. At this point, my users can insert the Gravity Form using the shortcode button on the content editor for the ‘connect-card’. I’d like to have a dropdown select field that would allow the post author to select the form from a list of forms available.

    Hopefully that offers a better explanation of what I’m trying to achieve. Thanks for the help!

    #13234
    Mark Tenney
    Participant

    Here’s a plugin I found that allows this functionality with ACF:

    https://github.com/stormuk/Gravity-Forms-ACF-Field

    #13330
    Anh Tran
    Keymaster

    Hi Mark, this looks like a custom field type. I found the Github repo above is useful and you can replicate it to work with Meta Box. This is the code to get list of forms and here is the tutorial on creating new field type.

    #18953
    Brian
    Participant

    Hi Anh or Mark,
    I need to do something similar. I have a “Select Advanced” dropdown field that I would like to populate with specific Gravity Forms, based on the form id. I reviewed the above links but still a little confused.

    Here is what I have so fare.

    if ( class_exists( 'RWMB_Field' ) ) {
        class RWMB_GFSelect_Field extends RWMB_Field {
            
        $forms = RGFormsModel::get_forms(1,2,3);
        $options = array();
        foreach ($forms as $form) {
            $options[$form->id] = $form->title;
        }
        return $options;
        
        }
    } 

    Am I creating a new select advance field or just extending it and telling it to pull in the specific gf forms? Not understanding.

    Thanks in advance!

    #18962
    Long Nguyen
    Moderator

    Hi Brian,

    That means you are creating a new/custom select field and pull the GF forms (name or ID) to save with the field.

    You can try to use the sample code to create a custom select field, show the dropdown GF form name and save the ID of the form

    add_action( 'init', function() {
        if ( class_exists( 'RWMB_Field' ) ) {
            class RWMB_GForm_Select_Field extends RWMB_Field {
                public static function html( $meta, $field ) {
                    $forms = GFAPI::get_forms();
                    $html = '';
                    $html .= '<label for="' . $field['id'] . '">Choose a form:</label>
                            <select id="' . $field['id'] . '" name="' . $field['field_name'] . '">';
                    foreach ( $forms as $id => $form ) {
                        $html .= '<option value="' . $id . '">' . $form['title'] . '</option>';
                    }
                    $html .= '</select>';
                    return $html;
                }
            }
        }
    
    } );
    
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'Select Form', 'text-domain' ),
            'id' => 'select-form',
            'post_types' => array(
                0 => 'post',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'gform_select',
                    'type' => 'gform_select',
                    'name' => esc_html__( 'Select Form', 'text-domain' ),
                ),
                
            ),
        );
    
        return $meta_boxes;
    }

    then you can show the GF form in the post content with the ID saved by getting the field value.

    For more information, please follow these documentations
    https://docs.gravityforms.com/adding-a-form-to-the-theme-file/
    https://docs.metabox.io/custom-field-type/

    #18973
    Brian
    Participant

    Hi Long,
    Thank you for the detailed reply. I see now what you did – creating a new select field. Makes sense and all is working. I’m good to go!

    Thanks again!

    #18997
    Brian
    Participant

    Hi Long,
    I spoke too soon. It is listing available Gravity Forms in the dropdown.

    But it’s not saving a value. For example if I select a form option other than the “first” in dropdown and save, when I go back to check the field it always displays the “first” form in listing. But it’s not saving any value to output.

    What do you think is missing?

    Thanks in advance!

    #19000
    Long Nguyen
    Moderator

    Hi,

    Sorry, it saves the value but not show the selected option, I’ve changed code to show the selected form

    add_action( 'init', function() {
        if ( class_exists( 'RWMB_Field' ) ) {
            class RWMB_GForm_Select_Field extends RWMB_Field {
                public static function html( $meta, $field ) {
                    $forms = GFAPI::get_forms();
    
                    // Get saved form ID
                    $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
                    $saved_form = rwmb_get_value( 'gform_select', '', $post_id );
    
                    $html = '';
                    $html .= '<label for="' . $field['id'] . '">Choose a form:</label>
                            <select id="' . $field['id'] . '" name="' . $field['field_name'] . '">';
                    foreach ( $forms as $id => $form ) {
                        if( $saved_form == $id ) {
                            $html .= '<option value="' . $id . '" selected >' . $form['title'] . '</option>';
                        } else {
                            $html .= '<option value="' . $id . '">' . $form['title'] . '</option>';
                        }                    
                    }
                    $html .= '</select>';
                    return $html;
                }
            }
        }
    
    } );

    Please check it out.

    #19002
    Brian
    Participant

    Hi Long,
    Thanks for the fast reply.The only issue now is that If I select the first form in dropdown and check the ID it outputs it always gives it a “0” id/value. But the id of the first GForm is “1” not zero. If I select the second form option in dropdown and save. The value is “1”, but the form id is really “2”.

    I’m wondering if I need to add a “Select a form” placeholder text within the dropdown field so that it takes the “0” value?

    Thoughts, what do you suggest? – hope I explained correctly.

    Thanks for your help!

    #19012
    Long Nguyen
    Moderator

    Hi,

    Please change the variable $id to $form['id']. To know all the properties of the form, you can use the function var_dump($form) to print it out.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
grid gear info copy caret-up caret-down trash