Support

Account

Home Forums ACF PRO Trick: retrieve Formidable Pro Forms in ACF

Solved

Trick: retrieve Formidable Pro Forms in ACF

  • I love ACF and Formidable Pro! But there was no way to get all available forms from formidable into an acf field.
    i dig into the frmpro code and found a helpful function.
    at the end this is my acf dropdown:
    https://www.dropbox.com/s/n6mwf9snkcgpr2j/Screenshot%202014-08-27%2015.41.33.png?dl=0
    it helps me to assign the ID of a form to a post.

    instructions:
    1. create some forms in formidable pro
    2. create a field from type “select”. the field name is important for the next step!
    3. insert the following code to your functions.php and modify it to your needs:

    /* helper function to get formidable forms to ACF: */
    function get_forms(){
    	ob_start();
    	FrmFormsHelper::forms_dropdown( 'frm_add_form_id' );
    	$forms = ob_get_contents();
    	ob_end_clean();
    	preg_match_all('/<option\svalue="([^"]*)" >([^>]*)<\/option>/', $forms, $matches);
    	$result = array_combine($matches[1], $matches[2]);
    	return $result;
    }
    /* auto populate acf field with form IDs */
    function load_forms_function( $field ){
    	$result = get_forms();
    	if( is_array($result) ){
    		$field['choices'] = array();
    		foreach( $result as $key=>$match ){
    			$field['choices'][ $key ] = $match;
    		}
    	}
        return $field;
    }
    add_filter('acf/load_field/name=FIELDNAME', 'load_forms_function');

    4. Change FIELDNAME with the name of your field. you can retrieve the assigned form id and use it this way:

    echo FrmFormsController::get_form_shortcode(
    	array(
    		'id' => get_field("FIELDNAME")
    	)
    );

    again change FIELDNAME to the name of your field!

    FINISHED!

    detailed description:
    I utilize the existing forms_dropdown function from frmpro. But it echos a select field. So i use ob_start to capture the output and assign it to a variable. then i use preg_match to get two arrays which contain the option value and the option text and merge them. in second function i use the array to autopopulate our acf field.

  • works great, thanks for the code and help!

  • Thanks, work great with last version!

  • I love this! Thanks for posting. Still works great!

  • Much appreciated the inspiration, but I modified the get_forms() function, because it is too much bound to the produced HTML. I prefer to rely directly on the database. Have not modified other snippet, so I’m thankful to @nicmare 🙂

    
    function get_forms(){
        $results = array();
        foreach (FrmForm::get_published_forms() as $published_form) {
            $results[$published_form->id] = $published_form->name;
        }
        return $results;
    }
    
  • i was not aware of “FrmForm::get_published_forms()”. Your code is definitely better 😉

  • Thanks a ton for this trick, it worked great!

    I took the liberty of mashing this function up with a Storm UK Gravity Forms select plugin, and made a Formidable Forms select plugin:

    https://github.com/iamhexcoder/formidable_select

    Just thought you’d like to know! Any feedback or issues, let me know.

    Props given to @nicmare and @welaika in the README 😉

  • Kudos for your zeal 🙂
    Happy to see a new open source piece of code out there.

    Cheers

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Trick: retrieve Formidable Pro Forms in ACF’ is closed to new replies.