Compare input field value with database value
Comparing WordPress database value with form input value
add_action( 'super_before_sending_email_hook', 'f4d_compare_database_value', 1 );
function f4d_compare_database_value($x){
extract(shortcode_atts(array('data'=>array(), 'form_id'=>0), $x));
$your_form_id = 12345;
if($form_id!==$your_form_id) return;
$data = wp_unslash($data);
// Our form has a field named `code` and we will lookup if this code exists in our custom databaset table named `wp_codes`
// Make sure to trim whitespaces at the start and end of the string
$code = trim(sanitize_text_field($data['code']['value']));
// Table structure that is used in this example:
// CREATE TABLE `db_name`.`wp_codes` (`id` INT NOT NULL AUTO_INCREMENT , `code` VARCHAR(50) NOT NULL , PRIMARY KEY (`id`), UNIQUE (`code`)) ENGINE = InnoDB;
global $wpdb;
$codesTable = $wpdb->prefix.'codes';
$sql = $wpdb->prepare("SELECT COUNT(*) FROM $codesTable AS c WHERE c.code = '%s'", $code);
$found = $wpdb->get_var($sql);
if(absint($found)===0){
// Code does not exists, return error
echo json_encode(
array(
'error' => true,
'fields' => array('code'), // field that contains errors
'msg' => 'Entered code is invalid, please try again'
)
);
die();
}
}Last updated