WPForms – theme locations

For more information, see Dynamic Dropdowns in ACF.

/**
 * WPForms theme locations
 *
 */
function be_wpforms_theme_locations() {

	return array(
		'newsletter-signup',
		'save-recipes-cta'
	);
}

/**
 * WPForms form data
 *
 */
function be_wpforms_form_data( $form_data ) {

	$theme_locations = be_wpforms_theme_locations();
	foreach( $theme_locations as $location ) {
		
		$form_id = get_option( 'options_' . $key );
		if( $form_id === $form_data['id'] )
			$form_data['settings']['form_class'] .= ' location-' . $location;
	}
	return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'be_wpforms_form_data' );

/**
 * Dynamically select form in ACF
 * @link https://www.billerickson.net/dynamic-dropdown-fields-in-acf/
 * @author Bill Erickson
 *
 * @param array $field, the field settings array
 * @return array $field
 */
function be_acf_wpforms_theme_locations( $field ) {

	// These are the field names we're updating
	$form_fields = be_wpforms_theme_locations();
	foreach( $form_fields as $i => $location ) {
		$form_fields[ $i ] = be_wpforms_acf_key( $location );
	}

	if( ! in_array( $field['name'], $form_fields ) )
		return $field;

	// Get form list from WPForms
	if( ! function_exists( 'wpforms' ) )
		return $field;
	$forms = wpforms()->form->get();

	// Build list of choices
	$field['choices'] = array( 0 => '(None)' );
	foreach( $forms as $form ) {
		$field['choices'][ $form->ID ] = $form->post_title;
	}
	return $field;
}
add_filter( 'acf/load_field', 'be_acf_wpforms_theme_locations' );

/**
 * WPForms ACF Key 
 *
 */
function be_wpforms_acf_key( $location ) {
	return 'ea_' . str_replace( '-', '_', $location );
}

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk