Examples: Some snippets for using eForm APIs

Disable Waypoints JS

// Completely deregister waypoints
add_action( 'ipt_fsqm_form_elements_front_enqueue', function() {
	wp_deregister_script( 'waypoints' );
}, 1 );

eForm Data to Array

<?php
// Somehow get the data Id you are after,
// perhaps from the request URI
$data_id = (int) ( $_GET['data_id'] ?? 0 );

// Some check if the data Id is okay
if ( 0 === $data_id ) {
	exit('Bad request!');
}

// create a new form elements data instance
$data = new IPT_FSQM_Form_Elements_Data( $data_id );
// check if data_id exists in database
if ( null === $data->data_id ) {
	exit( 'Bad id!' );
}

// everything okay, so start converting
$data_array = [
	'mcq' => $data->data->mcq, // mcq/choice elements
	'freetype' => $data->data->freetype, // freetype elements
	'pinfo' => $data->data->pinfo, // other elements
];

// Now you do what ever you want to do with data

eForm Score Manipulation

<?php

// A function to check for score on our custom element
function eform_custom_score_manipulation( IPT_FSQM_Form_Elements_Data $data ) {
	// check if this matches the form Id we are after
	if ( $data->form_id != 99 ) {
		return;
	}

	// Get the element data and calculate the score
	$element_data = $data->data->mcq['10'];
	$current_score = $data->data->score;
	$current_max_score = $data->data->max_score;

	// Say our element contributes a maximum score of 10
	// So increase the current max score
	$current_max_score += 10;

	// Now if the element data has a value of 'foo', then we add 5
	// if it has value of 'bar', we add 10.
	if ( $element_data['value'] === 'foo' ) {
		$current_score += 5;
	} elseif ( $element_data['value'] === 'bar' ) {
		$current_score += 10;
	}

	// Now update the database
	/** @var WPDB $wpdb  */
	global $wpdb, $ipt_fsqm_info;

	$wpdb->update(
		$ipt_fsqm_info['data_table'],
		[
			'score' => $current_score,
			'max_score' => $current_max_score,
		],
		[
			'id' => $data->data_id,
		],
		'%f',
		'%s'
	);
}

// add the function to the filters
add_filter( 'ipt_fsqm_hook_save_insert', 'eform_custom_score_manipulation', 10, 1 );
add_filter( 'ipt_fsqm_hook_save_update', 'eform_custom_score_manipulation', 10, 1 );

Custom action on Save

// - ipt_fsqm_hook_save_insert -
// - ipt_fsqm_hook_save_update -
add_action( 'ipt_fsqm_hook_save_insert', 'my_save_action', 10, 1 );
add_action( 'ipt_fsqm_hook_save_update', 'my_save_action', 10, 1 );
function my_save_action( $data ) {
	// Check for validity
	if ( ! ( $data instanceof IPT_FSQM_Form_Elements_Data ) ) {
		return;
	}

	// Check if it is an update or insert
	$doing_update = $data->doing_update;

	// Do something with the mcq submission data
	foreach ( $data->data->mcq as $m_key => $mcq ) {
		// Your code
	}

	// Do something with the freetype submission data
	foreach ( $data->data->freetype as $f_key => $freetype ) {
		// Your code
	}

	// Do something with the other elements submission data
	foreach ( $data->data->pinfo as $p_key => $pinfo ) {
		// Your code
	}

	// Other informative variables
	$user_meta = array(
		'f_name' => $data->data->f_name,
		'l_name' => $data->data->l_name,
		'email' => $data->data->email,
		'phone' => $data->data->phone,
		'score' => $data->data->score,
		'max_score' => $data->data->max_score,
		'user_id' => $data->data->user_id, // 0 when it is a gues (not logged in)
		'admin_remarks' => $data->data->comment,
		'date' => $data->data->date,
		'ip' => $data->data->ip,
	);

	// For accessing form related data
	// Please use the following member variables
	// $data->layout, $data->design, $data->mcq, $data->freetype, $data->pinfo
	// $data->settings, $data->name, $data->type
}

Swashata has written 257 articles

Hi there, I am the Lead Developer at WPQuark.com. I love to create something beautiful for WordPress and here I write about how to use them. When I am not working, usually I am doing a number of other creative things ;).

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>

This site uses Akismet to reduce spam. Learn how your comment data is processed.