Dynamic Fluent Form with ACF

tdrayson

Dynamically render a Fluent Form on the frontend using form ID & ACF.

Setup

  1. Create an ACF select field to choose from available Fluent Forms.
  2. Use a shortcode to dynamically display the selected Fluent Form.

Step 1: Create ACF Select Field

First, we’ll create a dynamic ACF select field that populates with all available Fluent Forms.

Add this code to your theme’s functions.php or a custom plugin:

PHP

        /**
 * Dynamically populate ACF select field with Fluent Forms.
 *
 * @param array $field The field array containing all settings for the field.
 * @return array The modified field array with populated choices.
 */
function acf_load_fluent_forms_choices($field) {
    // Reset choices
    $field['choices'] = array();

    // Get Fluent Forms
    $formApi = fluentFormApi('forms');
    $atts = [
        'status' => 'published',
        'sort_column' => 'id',
        'sort_by' => 'DESC',
        'per_page' => 100,
    ];
    $forms = $formApi->forms($atts, $withFields = false);

    // Process forms
    if (isset($forms['data']) && is_array($forms['data'])) {
        foreach ($forms['data'] as $form) {
            if (isset($form->id) && isset($form->title)) {
                $field['choices'][$form->id] = $form->title;
            }
        }
    }

    // Return the field
    return $field;
}

add_filter('acf/load_field/name=fluent_form', 'acf_load_fluent_forms_choices');
      
Copy

Then, create an ACF select field:

  1. Field Label: Fluent Form
  2. Field Name: fluent_form
  3. Field Type: Select

The choices will be dynamically populated with all published Fluent Forms.

Step 2: Create Dynamic Shortcode

Next, we’ll create a shortcode that uses the selected form ID from the ACF field.

Add this code to your functions.php or custom plugin:

PHP

        /**
 * Create a dynamic shortcode for Fluent Forms based on ACF field value.
 *
 * @param array $atts Shortcode attributes. Expected to contain a 'field' key
 *                    with the name of the ACF field storing the form ID.
 * @return string The generated Fluent Form shortcode, or an empty string if no form ID is found.
 */
function tct_dynamic_fluentform_shortcode($atts){
    $id = get_field($atts['field']);
    if(!$id){
        return '';
    }
    return do_shortcode(sprintf('', $id));
}
add_shortcode( 'dynamic_fluentform', 'tct_dynamic_fluentform_shortcode' );
      
Copy

How to Use

  1. In your WordPress admin, go to the page or post where you want to display the form.
  2. Use the ACF field ‘Fluent Form’ to select the desired form.
  3. In the content editor, add the following shortcode:
[dynamic_fluentform field="fluent_form"]

This shortcode will dynamically render the Fluent Form you selected in the ACF field.

Original Question: https://www.facebook.com/groups/fluentforms/posts/1040580730084209/

Resources:
https://developer.wordpress.org/reference/functions/do_shortcode/
https://developer.wordpress.org/reference/functions/add_shortcode/

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise