Populate ACF dropdown with Fluent forms

tdrayson

A user in the Fluent Form Facebook group wanted to dynamically populate all Fluent Forms to an ACF field dropdown so that they could choose which form to render on their template dynamically.

PHP

        <?php

if (!function_exists("tct_select_fluent_form")) {
    add_filter(
        "acf/load_field/name=YOUR_ACF_FIELD",
        "tct_select_fluent_form"
    );
    function tct_select_fluent_form($field)
    {
        // reset choices
        $field["choices"] = [];

        $formApi = fluentFormApi("forms");
        $atts = [
            "status" => "all",
            "sort_column" => "title",
            "sort_by" => "ASC",
            "per_page" => 500,
        ];
        $forms = $formApi->forms($atts, $withFields = false);

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

        return $field;
    }
}
      
Copy

Notes

Replace YOUR_ACF_FIELD with the name of your acf select dropdown key

Forms will load sorted by Title in ASC order (A to Z)

Add snippet to functions.php or a code snippets plugin

Sources

https://www.scratchcode.io/dynamically-populate-a-select-fields-choices-in-acf/
https://fluentforms.com/docs/fluent-form-php-api/

👋🏻 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