Created
May 28, 2018 21:32
-
-
Save xnau/73bba4c5a717ec240bbab174a13d1e12 to your computer and use it in GitHub Desktop.
Shows how to send a Participants Database private link by matching 2 or more fields
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 Custom Link Recovery | |
* Description: sets up a private link recovery using two or more fields to find the record | |
*/ | |
add_filter( 'pdb-find_record_match', 'xnau_custom_link_recovery', 10, 3 ); | |
/** | |
* finds a match for a link recovery submission | |
* | |
* @global wpdb $wpdb | |
* | |
* @param int|bool $matched_id the id found using the standard method, bool | |
* false if no match was found | |
* @param string $columns column name or names used to find the match | |
* @param array $submission the un-sanitized $_POST array | |
* | |
* @return int|bool the found record ID | |
*/ | |
function xnau_custom_link_recovery ( $matched_id, $columns, $submission ) | |
{ | |
// test to make sure it is the correct form | |
if ( $submission['action'] !== 'retrieve' ) { | |
return $matched_id; | |
} | |
global $wpdb; | |
// this array names the fields to test for a match | |
$match_fields = array( 'email', 'first_name' ); | |
$matches = array(); | |
$values = array(); | |
foreach( $match_fields as $field ) { | |
// add the match clause to the query | |
$matches[] = '`' . $field . '` = %s'; | |
// add the value to the values array | |
$values[] = isset( $submission[$field] ) ? $submission[$field] : ''; | |
} | |
// set up the query | |
$sql = 'SELECT `id` FROM ' . Participants_Db::$participants_table . ' WHERE ' . implode( ' AND ', $matches ); | |
// get the results of the match | |
$result = $wpdb->get_var( $wpdb->prepare( $sql, $values ) ); | |
// return the result if it is an ID, false otherwise | |
return is_numeric( $result ) ? $result : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installing a plugin from a GitHub Gist:
https://xnau.com/how-to-install-a-wordpress-plugin-from-a-gist/