The form below has a date field, a time field, and a numerical field.

Using JavaScript, I've made a change in the time field trigger code to check if the time is less than 24 hours in the future. This can be used to add a premium to last-minute bookings, disable submission, whatever you need to have happen.

In the current example, a message is shown and the value in the numerical field is changed if the date/time selected is less than 24 hours in the future.

24 Hour Limit Test

The code to accomplish this is below:

(function($){
    // Set variable to current date and time
    const now = new Date();
    let stamp = now.getTime();
    let tomorrow = stamp + (86400000);
    $('#field_time').change(function(){ 
        let date_array = $('#field_date').val().split('/');
        let time_array = $('#field_time').val().split(':');
        const time_slot  = new Date(date_array[2],(date_array[0] - 1),date_array[1],time_array[0],time_array[1]);
        let time_stamp = time_slot.getTime();
        if( time_stamp < tomorrow ) {
            $('#field_price').val(100);
            $('#field_notif').html('Your selected time is less than 24 hours in the future. Last-minute bookings are subject to a surcharge.');
        } else {
            $('#field_price').val(50);
            $('#field_notif').html('Your selected time provides adequate notice, so no surcharge will be levied.');
        }
    });
})(jQuery)

Sometimes you have a Search form with a repeating section in it, and are using 'frm_get_default_value' to populate it, but the darn thing won't show the right number of rows. This fixes it.

add_filter( 'frm_repeat_start_rows', 'frm_set_ten_rows', 10, 2);
function frm_set_ten_rows( $row_num, $field ){
	if ( $field['id'] == 157 && isset($_REQUEST['depdob']) ) { // change 157 to the id of your repeating section
		$myarray = explode(', ',$_REQUEST['myvar']); // change myvar to your get param
		$row_num = count($myarray);
	}
	return $row_num;
}

Sometimes you just want to set the number of rows that show to a fixed number, like this:

add_filter( 'frm_repeat_start_rows', 'frm_set_ten_rows', 10, 2);
function frm_set_ten_rows( $row_num, $field ){
	$row_num = 3; // set 3 to whatever you want
	return $row_num; // or just return a number and make it one line
	// I only did it this way to make what's happening clear
}

Sometimes you need to calculate a person's age on the fly, but on a given date, not today's.

This is especially useful in Formidable Pro Views, where you can use Formidable shortcodes to populate the date variables.

/********************************************************************************
 * erentzen@gmail.com
 * function calculate_years_diff($atts = array( 'birthdate' => 'Y-m-d','effective' => 'Y-m-d')
 * calculates difference in years between two dates
 * can be called by shortcode 
 ********************************************************************************/
add_shortcode('calculate_age','calculate_years_diff');
function calculate_years_diff($atts) {
	$defaults = array(
		'birthdate' => false,
		'effective' => false
	);
	$atts = shortcode_atts($defaults, $atts, 'calculate_age');
	extract($atts);
	if( false === $birthdate || false === $effective ) {
		return;
	} else {
		
		$birthdate 	= new DateTime( $birthdate );
		$effective 	= new DateTime( $effective );	
		$interval 	= $birthdate->diff($effective);
		$age 		= $interval->y;
	}
	
	return $age;
}

Sometimes a form has thousands of entries, or even tens of thousands. Deleting them manually via the admin interface is a bit much in these circumstances, since you can only do up to 999 at a time this way.

You can use PHPmyAdmin or a similar database tool to do it all in one fell swoop, or use the queries below in a PHP function.

DELETE FROM `wp_frm_items` WHERE `form_id` = YOUR FORM ID
DELETE FROM `wp_frm_item_metas` WHERE `field_id` IN (COMMA-DELIMITED LIST OF FIELD IDS BELONGING TO THE FORM)

That's it!

Sometimes when you're populating a dynamic field with information from another form's entries, you only want certain ones - like entries where another field matches some criteria or other.

In cases like that, do this:

function filter_dfe_options($values, $field){
	// Filter dfe entries list to only include 'something' entries
    if ( $field->id == 1181 && !empty( $values['options'] ) ) { //Replace 1181 with the ID of your dfe field
        $temp = $values;
        $temp['form_select'] = 30; //change 30 to the id of the field (in linked form) that you want to filter by
        $field2_opts = FrmProFieldsHelper::get_linked_options( $temp, $field );
        foreach ( $values['options'] as $id => $v ) {
            if ( isset( $field2_opts[$id] ) && ( $v == '' || $field2_opts[$id] == 'something' ) ) {
              // Optional: only include values where filtering field equals "something"
                continue;
            }
            unset( $values['options'][$id] );
        }
    }
    return $values;
}
add_filter('frm_setup_new_fields_vars', 'filter_dfe_plan_options', 25, 2);

/*
function change_view_order()

applied whenever a view is called.

Allows ANY view to be sorted based on query string ?sort={field ID here}&order={ASC|DESC|RAND} here

*/
add_filter('frm_view_order', 'change_view_order', 10, 2);
function change_view_order($query, $args){
	if( isset($_REQUEST['sort']) && is_numeric($_REQUEST['sort']) ) {
		if(isset($_REQUEST['order']) ) {
			$order = $_REQUEST['order'];
		} else {
			$order = "ASC";
		}
		$query['select'] .= ' LEFT JOIN wp_frm_item_metas em1 ON em1.item_id=it.id AND em1.field_id='.$_REQUEST['sort'];
		$query['order'] = 'ORDER BY em1.meta_value '.$_REQUEST['order'];
		
	}
	return $query;
}

Put this in your functions.php or a plugin:

add_action('wp_ajax_nopriv_bsn_search_users','bsn_search_users');
function  bsn_search_users() {
	$user = get_user_by( 'email', $_REQUEST['user_email'] );
	if(false === $user) { // user doesn't exist
		// do something like:
		echo 'email not found'; // replace 'yourstatusmessageclass' with some class you define to hold the message
		exit();
	} else {
		// show the form
		echo ""; // replace 'form_wrapper_div' with the id of a div you place the form in
		exit();
	} 
}

Then in your page, go:



The Best Practice for Formidable to ensure the portability of your code along with Forms and Views requires you to use keys instead of IDs absolutely everywhere.

This is all well and good, but the code examples in the official documentation always seem to reference IDs, and in most cases, the ID of a view, field or form is required in order to do anything useful in code.

The functions below (or something like them) are therefore necessary in order to enable the global use of keys in code. I use them all over the place.

/*
 -----------------------------------------------------------------------------------
 UTILITY FUNCTIONS
 -----------------------------------------------------------------------------------
 The functions below are all descriptively named and each serve a single purpose.
*/

if (!function_exists('bsn_edit_dropdown_from_key')) {
	function bsn_edit_dropdown_from_key($atts) {
		$id = bsn_get_form_id_from_key($atts['form_key']);
		$field = $atts['field_key'];
		$page_id =$atts['page_id'];
		
		return do_shortcode('');
	}
	add_shortcode('bsn_edit_dropdown_from_key','bsn_edit_dropdown_from_key'); // shortcode definition MUST come AFTER function in "if(!function_exists()) {}" for some reason.
}
if (!function_exists('bsn_get_form_id_from_field_key')) {
	function bsn_get_form_id_from_field_key($field_key) {
		// get form id given a field key as imput
		global $frmdb, $wpdb;
		$form_id = $wpdb->get_var("SELECT form_id FROM $frmdb->fields WHERE field_key LIKE '".$field_key."'");
		return $form_id;
	}
}

if (!function_exists('bsn_get_form_id_from_key')) {
	function bsn_get_form_id_from_key($form_key) {
		// get form id given a form key as imput
		if(is_array($form_key)) {
			$form_key = $form_key['key'];
		}
		global $frmdb, $wpdb;
		$form_id = $wpdb->get_var("SELECT id FROM $frmdb->forms WHERE form_key LIKE '".$form_key."'");
		return $form_id;
	}
}

if (!function_exists('bsn_get_field_id_from_key')) {
	function bsn_get_field_id_from_key($field_key) {
		global $frmdb, $wpdb;
		$field_id = $wpdb->get_var("SELECT id FROM $frmdb->fields WHERE field_key LIKE '".$field_key."'");
		return $field_id;
	}
}

if (!function_exists('bsn_get_all_entries_in_form')) {
	function bsn_get_all_entries_in_form($form_id) {
		global $frmdb, $wpdb;
		$entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE form_id=".$form_id);
		return $entry_ids;
	}
}

if (!function_exists('bsn_get_users_array')) {
	function bsn_get_users_array(){
		$args = array(
			'orderby'      => 'ID',
			'order'        => 'ASC',
			'fields'       => 'all',
		);
		$bsn_users = get_users( $args );
		return $bsn_users;
	}
}

/*
 * function bsn_get_child_entry_ids($parent_entry_id,$child_form_id=NULL)
 *
 * returns a one-dimensional array of entry ids of child form entries
 *
 * optionally specify child form id to get entries from a specific child form if there are several
 *
 */
 
if (!function_exists('bsn_get_child_entry_ids')) {
	function bsn_get_child_entry_ids($parent_entry_id,$child_form_id=NULL) {
		global $wpdb,$frmdb;
		if($child_form_id != NULL) {
			$child_entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE parent_item_id=$parent_entry_id AND form_id=$child_form_id ORDER BY id ASC");
		} else { // if no child form id is specified, grab all child form entries willy-nilly
			$child_entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE parent_item_id=$parent_entry_id ORDER BY id ASC");
		}
		return $child_entry_ids;	
	}
}

if (!function_exists('bsn_get_post_id_from_slug')) {
	function bsn_get_post_id_from_slug($slug) {
		global $wpdb;
		$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'");
		return $id;
	}
}
add_filter('frm_where_filter', 'my_unique_name_filter_custom_display', 10, 2);
function my_unique_name_filter_custom_display($where, $args){
	$view_id = bsn_get_post_id_from_slug('slug-of-view'); // replace 'slug-of-view' with your view slug
	$field_id = bsn_get_field_id_from_key('field-key'); // replace 'field-key' with your field's key
	if ( $args['display']->ID == $view_id && $args['where_opt'] == $field_id){ 
		$user_id = $_GET['trainer']; // change trainer to the name of the parameter in your URL
		if ( !is_numeric($user_id) ) {
			// if the username was in the url, get the ID
			$user_id = FrmProAppHelper::get_user_id_param($user_id);
		}
		if ( is_numeric($user_id) ) {
			$where = ("meta_value = ". $user_id ." and fi.id='". $args['where_opt'] ."'");
		} else {
			// no matching user ID was found
			$where = ("meta_value = 1 and meta_value = 0 and fi.id='". $args['where_opt'] ."'");
		}
	}
	return $where;
}

if (!function_exists('bsn_get_post_id_from_slug')) {
	function bsn_get_post_id_from_slug($slug) {
		global $wpdb;
		$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'");
		return $id;
	}
}

if (!function_exists('bsn_get_field_id_from_key')) {
	function bsn_get_field_id_from_key($field_key) {
		global $frmdb, $wpdb;
		$field_id = $wpdb->get_var("SELECT id FROM $frmdb->fields WHERE field_key LIKE '".$field_key."'");
		return $field_id;
	}
}

[vc_row][vc_column][vc_column_text]Use this code to change a user’s role after he/she submits an entry in a specific form.

/**
* This will change an inactive user to a member after they complete their member profile.
*/
add_action('frm_after_create_entry', 'inactive_to_member', 20, 2);
function inactive_to_member($entry_id, $form_id){
	if($form_id == 24){ //change 24 to the form id of the form to copy
	$new_role = 'member'; //change this to the role users should be granted upon completing form

	$user = wp_get_current_user(); //get logged in user
	if(!$user) {
		return; //don't continue if user doesn't exist
	}

	$updated_user = (array)$user;

	// Get the highest/primary role for this user  
	$user_roles = $user->roles;
	$user_role = array_shift($user_roles);
	if ( $user_role == 'administrator' ) 
		return; //make sure we don't downgrade any admins

		$updated_user['role'] = $new_role;

		wp_update_user($updated_user);
	}
}

[/vc_column_text][/vc_column][/vc_row]

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

Email sending image

You have Successfully Subscribed!