3

Populate ACF select field options with Gravity Forms to select a specific form

 3 weeks ago
source link: https://gist.github.com/psaikali/2b29e6e83f50718625af27c2958c828f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Populate ACF select field options with Gravity Forms to select a specific form · GitHub

Instantly share code, notes, and snippets.

Clone this repository at <script src="https://gist.github.com/psaikali/2b29e6e83f50718625af27c2958c828f.js"></script>
Save psaikali/2b29e6e83f50718625af27c2958c828f to your computer and use it in GitHub Desktop.
Populate ACF select field options with Gravity Forms to select a specific form

Thank you very much! It does exactly what I need but I have one remaining question: How would I be able to have a first (default) selection that is empty, so I can have no form when none is selected? Basically I need the FIRST select option to be "None", defaulted to the value 0 ...
THANKS so much!

Thank you very much! It does exactly what I need but I have one remaining question: How would I be able to have a first (default) selection that is empty, so I can have no form when none is selected? Basically I need the FIRST select option to be "None", defaulted to the value 0 ...
THANKS so much!
@zartgesotten
You can add something like:
$choices[ $form->id ] = 'Select a form';
Before the "foreach" loop.

This is very elegant. Thanks a lot!

This is great, such a simple solution I thought I'd be trying to figure this out all night. Thanks!

I just come here to say thanks 👍

Could somebody explain how this works?

I've added the code to functions.php and I was expecting to see a "Gravity Forms" option in the field type selector.

Thanks.

[edit] I've worked out how it's done. Just use a normal ACF 'Select' field. Then add this field's field name after "/load_field/" in the add_filter call.

Author

Glad you found the solution @KingWebsites, and thank you for sharing it!

Thanks. And you can use this if you'd like to use this function in a exported PHP file:

if (class_exists('GFFormsModel')) {
    $choices = [];
    foreach (\GFFormsModel::get_forms() as $form) {
        $choices[$form->id] = $form->title;
    }
}

acf_add_local_field(array(
    'key' => 'contact_form',
    'label' => 'Contact form',
    'name' => 'contact_form',
    'type' => 'select',
    'instructions' => '',
    'required' => 0,
    'conditional_logic' => 0,
    'choices' => $choices,
    'default_value' => array(),
    'allow_null' => 0,
));

Great solution! How do i display the selected form on my page? The value from the select box is the form ID.

Thank you psaikali! This was so helpful for me. I was getting a php warning (running php 8+) with the original code, so I'm offering this version that worked for me.

*Note to others: remember to change 'your_field_name' to whatever you named your ACF Select field.


// Populate ACF select field options with Gravity Forms forms 
function acf_populate_gf_forms_ids( $field ) {
    if ( class_exists( 'GFFormsModel' ) ) {
        $choices = array(
            'none' => 'None'
        );

        $forms = \GFFormsModel::get_forms();

        if ( $forms ) {
            foreach ( $forms as $form ) {
                $choices[ $form->id ] = $form->title;
            }
        }

        $field['choices'] = $choices;
    }

    return $field;
}
add_filter( 'acf/load_field/name=your_field_name', 'acf_populate_gf_forms_ids' );


Here's the PHP code to render the shortcode:

<?php 
	$form_id = get_field('your_field_name');
	if ($form_id && $form_id !== 'none') {
	    $escaped_form_id = acf_esc_html($form_id);
	    echo do_shortcode("[gravityform id=\"$escaped_form_id\" title=\"true\" description=\"true\" ajax=\"true\" tabindex=\"4\" theme=\"gravity\"]");
	}
?>

*Edited to set the default field to "none"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK