Skip to content

Instantly share code, notes, and snippets.

@xnau
Created June 22, 2021 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/bdaeb93aa6cba4af6401a85c5bcc242a to your computer and use it in GitHub Desktop.
Save xnau/bdaeb93aa6cba4af6401a85c5bcc242a to your computer and use it in GitHub Desktop.
Shows how to set up an alternate record ID scheme in Participants Database
<?php
/*
* Plugin Name: Participants Database Alternate ID Field
* Version: 0.1
* Description: allows the use of a custom field for the general ID that is used to show a record
* Author: xnau webdesign
*/
class PDb_alternate_id_field {
/**
* @var string name of the alternate id field
*
* fill this in with the name of the field you want to use to hold your alternate ID
*/
private $alt_id_field = 'customer_id';
/**
* @var string GET variable name
*
* this is the name used in the URL to identify your ID value
*/
const get_var = 'custid';
/**
* sets up the filters
*/
public function __construct()
{
// modify the single record URL to use the alternate ID
add_filter( 'pdb-single_record_url', array( $this, 'single_record_url' ), 50, 2 );
/* provide the internal record ID to the shortcode
* this translates the alternate ID to the internal id
*/
add_filter( 'pdb-shortcode_call_pdb_single', array( $this, 'get_record_id' ) );
}
/**
* alter the single record URL
*
* @param string $URL
* @param int $record_id
* @return string URL
*/
public function single_record_url( $URL, $record_id )
{
return Participants_Db::add_uri_conjunction( Participants_Db::single_record_page() ) . self::get_var . '=' . urlencode( $this->get_record_alt_id( $record_id ) );
}
/**
* provides the record ID to the shortcode
*
* @param array $params the shortcode parameters
* @return array parameter array
*/
public function get_record_id( $params )
{
$alt_id = $this->get_alt_id();
if ( $alt_id ) {
$params['record_id'] = $this->_get_record_id($alt_id);
}
return $params;
}
/**
* gets the alternate id from the URL
*
* @return string
*/
private function get_alt_id()
{
return filter_input( INPUT_GET, self::get_var, FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE );
}
/**
* provides the alternate ID value, given the record id
*
* @global wpdb $wpdb
* @param int $record_id
* @return string the alternate ID for the record, empty string if not found
*/
private function get_record_alt_id( $record_id )
{
global $wpdb;
$alt_id = $wpdb->get_var( $wpdb->prepare( 'SELECT p.' . $this->alt_id_field . ' FROM ' . Participants_Db::participants_table() . ' p WHERE p.id = %s', urldecode( $record_id ) ) );
return is_null($alt_id) ? '' : $alt_id;
}
/**
* provides the record id, given the alt ID
*
* @global wpdb $wpdb
* @param int $alt_id
* @return int the record ID value, 0 if not found
*/
private function _get_record_id( $alt_id )
{
global $wpdb;
$record_id = $wpdb->get_var( $wpdb->prepare( 'SELECT p.id FROM ' . Participants_Db::participants_table() . ' p WHERE p.' . $this->alt_id_field . ' = %s', $alt_id ) );
return is_null($record_id) ? 0 : $record_id;
}
}
new PDb_alternate_id_field();
@xnau
Copy link
Author

xnau commented Jun 22, 2021

Tutorial plugin from the Using a Custom Identifier Value for Participants Database Records post on xnau.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment