WPForms – theme locations

For more information, see Dynamic Dropdowns in ACF.

/**
 * WPForms theme locations
 *
 */
function be_wpforms_theme_locations() {
	return [ 'header', 'footer' ];
}
/**
 * WPForms theme locations, class
 * @see https://www.billerickson.net/code/wpforms-theme-locations/
 */
function be_wpforms_theme_locations_class( $form_data ) {
	$locations = be_wpforms_theme_locations();
	if( empty( $locations ) )
		return $form_data;

	foreach( $locations as $location ) {
		$form_id = get_option( 'options_be_' . $location . '_form' );
		if( !empty( $form_id ) && $form_id == $form_data['id'] )
			$form_data['settings']['form_class'] .= ' wpforms-location-' . $location;
	}
	return $form_data;
}
add_filter( 'wpforms_frontend_form_data', 'be_wpforms_theme_locations_class' );

/**
 * WPForms admin column
 *
 */
function be_wpforms_theme_locations_column( $columns ) {

	$locations = be_wpforms_theme_locations();
	if( empty( $locations ) )
		return $columns;

	$new_columns = [];
	foreach( $columns as $key => $value ) {
		$new_columns[ $key ] = $value;
		if( 'form_name' === $key ) {
			$new_columns[ 'be_theme_location' ] = __( 'Theme Location', 'cultivate_textdomain' );
		}
	}
	return $new_columns;
}
add_filter( 'wpforms_overview_table_columns', 'be_wpforms_theme_locations_column' );

/**
 * WPForms admin column value
 *
 */
function be_wpforms_theme_locations_column_value( $value, $form, $column_name ) {
	if( 'be_theme_location' !== $column_name )
		return;

	$current = [];
	$locations = be_wpforms_theme_locations();
	foreach( $locations as $location ) {
		$form_id = get_option( 'options_be_' . $location . '_form' );
		if( !empty( $form_id ) && $form->ID == $form_id )
			$current[] = $location;
	}

	if( !empty( $current ) )
		echo ucwords( str_replace( '_', ' ', join( ', ', $current ) ) );
}
add_filter( 'wpforms_overview_table_column_value', 'be_wpforms_theme_locations_column_value', 10, 3 );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk