NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Table-WordPress-–-Enter
Intermediate

---- Last modified : Oct 30 2022

Estimated reading time : 4 minutes, 13 seconds - 288 words

You want to create a new data entry in a custom WordPress table from a Formidable form? Let’s say you want to create a table in order to get data from a form or generate stats.

Once your table has been created with the required columns, you need to make sure that the columns matches your form’s fields.

If you want to create a table with a function, here’s an interesting article that explains a function that will create a WordPress table

Create a table from data obtained via a Formidable form
//////////////////////////////////////////////////////////////////////
// Create database table
//////////////////////////////////////////////////////////////////////

function database_wooclassementstats() {
	global $wpdb;
	$charset_collate = $wpdb->get_charset_collate();
	$table_name = $wpdb->prefix . 'wooclassementstats';
		$sql = "CREATE TABLE $table_name (
		id mediumint(9) NOT NULL AUTO_INCREMENT,
		viewdate date DEFAULT '0000-00-00' NOT NULL,
		viewtime time DEFAULT '00:00:00' NOT NULL,
		productid int(11) NOT NULL,
		productname varchar(60) NOT NULL,
		producturl text NOT NULL,
		userip varchar(60) NOT NULL,
		username varchar(60) NOT NULL,
		userid bigint(20) NOT NULL,
		usercountry text NOT NULL,
		usercity varchar(255) NOT NULL,
		userbrowser varchar(255) NOT NULL,
		userbrowserversion varchar(255) NOT NULL,
		userbrowserplateform varchar(255) NOT NULL,	
		userviews smallint(5) NOT NULL,
		userclicks smallint(5) NOT NULL,
		urlfrom text NOT NULL,
		type varchar(60) DEFAULT 'vue' NOT NULL,
		language varchar(60) NOT NULL,
		UNIQUE KEY id (id)
		) $charset_collate;";
	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	dbDelta( $sql );
}	
add_action( 'init', 'database_wooclassementstats');

Here’s our table within phpMyAdmin.

Possible web outage due to WPML

As you can see there are multiple columns in our WordPress table.

First, change the name of the table, wooclassementstats to the naming convention of your table.

You’ll also need to replace the columns’ names and adapting them to the data you’re looking for.

Adding Formidable fields data

For that we’re gonna need the hook Formidable Form called frm_after_create_entry

Within our hook, we will add a condition where it’ll need to fetch data from the form according to the specific form ID.

The $values variable includes all the data fetched from the form. Change the columns’ names and IDs of the matching fields for each field.

add_action('frm_after_create_entry', 'copy_into_my_table', 20, 2);
function copy_into_my_table($entry_id, $form_id){ 
  if($form_id == 2){ //change 4 to the form id of the form to copy
    global $wpdb;
    $values = array(
    'col_name'         => $_POST['item_meta'][24], 
    'col_name'         => $_POST['item_meta'][25], 
    'col_name'         => $_POST['item_meta'][22],
    'col_name'         => $_POST['item_meta'][21],
    'col_name'         => $_POST['item_meta'][26],
    'col_name'         => $_POST['item_meta'][23],
    'col_name'         => '',
    'col_name'         => '', 
    'col_name'         => $_POST['item_meta'][27],
    );
    //replace 25 and 26 with the field ids of the Formidable form. Change col_name to the column names in your table
    $wpdb->insert('wpeo_LDAAnalytics', $values); 
  }
}

For Multi-sites websites

If you have a multisite, first thing you’ll need to do is make sure all forms have their own unique IDs. You will need the switch_to_blog function.

Our form’s condition will need to be set between switch_to_blog and restore_current_blog(); which will respect the blog’s query.

//////////////////////// Insert datas after create entry from Form  /////////////////////

add_action('frm_after_create_entry', 'copy_into_my_table', 20, 2);
function copy_into_my_table($entry_id, $form_id){ 
	switch_to_blog(2);
	if($form_id == 2){ //change 4 to the form id of the form to copy
		global $wpdb;
		$values = array(
		'col_name'         => $_POST['item_meta'][24], 
		'col_name'         => $_POST['item_meta'][25], 
		'col_name'         => $_POST['item_meta'][22],
		'col_name'         => $_POST['item_meta'][21],
		'col_name'         => $_POST['item_meta'][26],
		'col_name'         => $_POST['item_meta'][23],
		'col_name'         => '',
		'col_name'         => '', 
		'col_name'         => $_POST['item_meta'][27],
		);
		//replace 25 and 26 with the field ids of the Formidable form. Change col_name to the column names in your table
		$wpdb->insert('wpeo_LDAAnalytics', $values); 
	}
	restore_current_blog();
}

In conclusion, injecting data in a WordPress table will allow you to have duplicates of the data or offering stats to your clients.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>