Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Populate ACF select field options with Gravity Forms to select a specific form
<?php
/**
* Populate ACF select field options with Gravity Forms forms
*/
function acf_populate_gf_forms_ids( $field ) {
if ( class_exists( 'GFFormsModel' ) ) {
$choices = [];
foreach ( \GFFormsModel::get_forms() as $form ) {
$choices[ $form->id ] = $form->title;
}
$field['choices'] = $choices;
}
return $field;
}
add_filter( 'acf/load_field/name=submit_project_gf_form_id', 'acf_populate_gf_forms_ids' );
@zartgesotten

This comment has been minimized.

Copy link

@zartgesotten zartgesotten commented Apr 25, 2019

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!

@gargiguy

This comment has been minimized.

Copy link

@gargiguy gargiguy commented May 22, 2019

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.

@josfaber

This comment has been minimized.

Copy link

@josfaber josfaber commented Jan 22, 2020

This is very elegant. Thanks a lot!

@bheffron

This comment has been minimized.

Copy link

@bheffron bheffron commented May 22, 2020

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

@isuke01

This comment has been minimized.

Copy link

@isuke01 isuke01 commented Dec 10, 2020

I just come here to say thanks 👍

@KingWebsites

This comment has been minimized.

Copy link

@KingWebsites KingWebsites commented Mar 3, 2021

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.

@psaikali

This comment has been minimized.

Copy link
Owner Author

@psaikali psaikali commented Mar 4, 2021

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

@sjaakbanaan

This comment has been minimized.

Copy link

@sjaakbanaan sjaakbanaan commented Mar 31, 2021

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,
));
@SjorsHartwijk

This comment has been minimized.

Copy link

@SjorsHartwijk SjorsHartwijk commented Apr 28, 2021

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

@psaikali

This comment has been minimized.

Copy link
Owner Author

@psaikali psaikali commented Apr 28, 2021

Hey @SjorsHartwijk

You're looking for the gravity_form() function, see https://docs.gravityforms.com/adding-a-form-to-the-theme-file/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment