Shows how to set up a duplicate record check on a link-type field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Check Link Field for Duplicate | |
* Description: make sure a new record does not match the link of an existing record | |
* Version: 1.1 | |
*/ | |
// this filter checks the submission | |
add_action( 'pdb-incoming_record_match_object', 'xnau_check_for_link_match' ); | |
// set up our custom feedback message | |
add_filter( 'pdb-validation_error_messages', 'xnau_check_for_match_feedback' ); | |
/** | |
* checks the incoming submission for a match in the database | |
* | |
* @global wpdb $wpdb | |
* @param \PDb_submission\incoming_record_match $record_match the record match object | |
*/ | |
function xnau_check_for_link_match( $record_match ) | |
{ | |
// set this to the name of the "link" field you want to check | |
$check_field = 'link'; | |
// assume no match | |
$match_status = false; | |
// this is the submitted data | |
$post = $record_match->post; | |
/* | |
* don't include the record id to prevent attempting to add a new record with | |
* the same id as an existing record | |
* | |
*/ | |
if ( isset( $post['id'] ) ) { | |
unset( $post['id'] ); | |
} | |
// build the query to find our match | |
$sql = 'SELECT id FROM ' . Participants_Db::$participants_table . ' WHERE `' . $check_field . '` LIKE "%\"' . $post[$check_field][0] . '\"%"'; | |
global $wpdb; | |
$result = intval( $wpdb->get_var( $sql ) ); | |
// log the query and results if debugging | |
if (PDB_DEBUG) { | |
ob_start(); | |
var_dump($result); | |
Participants_Db::debug_log(__FUNCTION__.' match mode: '.$record_match->match_mode().' | |
query: '. $wpdb->last_query . ' | |
result: '. ob_get_clean() ); | |
} | |
/* | |
* the $result will be the id of the matched record or 0 if no match | |
*/ | |
if ( $result > 0 ) { | |
$record_match->set_record_id( $result ); | |
// we found a match set status to true | |
$match_status = true; | |
} | |
$record_match->set_match_status( $match_status ); | |
} | |
/** | |
* adds a feedback message for signup submissions | |
* | |
* this message will be shown if the signup is rejected because it matches an | |
* existing record | |
* | |
* @param array $messages | |
* @return array | |
*/ | |
function xnau_check_for_match_feedback( $messages ) | |
{ | |
// clear the error CSS so the wrong field does not get highlighted | |
add_filter( 'pdb-error_css', function ($css) { | |
return ''; | |
} ); | |
// set the feedback message | |
$messages['duplicate'] = 'The URL you submitted matches an existing record.'; | |
return $messages; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can download and install this as a plugin:
How to Install a WordPress Plugin from a Gist