Membership ID generation

Hi all. I have some code that was provided to me my a Wordpress plugin developer that generate membership IDs for members in a database. However, I need to change the format of the membership ID as I don’t want it to just be 4 digits. The current code is as follows:

<?php

/**
 * Plugin Name: PDB Generate Member ID
 * Description: provides a unique ID for new signups and new records in Participants Database
 * Version: 2.0
 */
class pdb_generate_member_id {

  /**
   * @var string name of the member id field
   */
  private $id_field = 'member_id';

  /**
   * @var string the starting member ID value
   */
  private $start_value = 1000;

  /**
   * initializes the plugin
   */
  public function __construct()
  {
    // this is used when a signup form is submitted
    add_filter( 'pdb-after_submit_signup', array($this, 'maybe_save_member_id') );
    
    // this is used when a new record is added from the backend
    add_filter( 'pdb-after_submit_add', array($this, 'maybe_save_member_id') );

    // this is used when a new record is added from the backend
    add_filter( 'pdb-after_submit_update', array($this, 'maybe_save_member_id') );
  }
  
  /**
   * saves the new member ID
   * 
   * this is called after the record is added or updated
   * 
   * @param array $record the submitted data
   */
  public function maybe_save_member_id( $record )
  {
    if ( $this->record_needs_id( $record ) ) {
      $this->store_member_id( $record['id'], $this->generate_new_member_id() );
    }
  }
  
  /**
   * checks the record to see if it needs a member ID assigned
   * 
   * @param array $record the incoming record values
   * @return bool true if the record needs an ID
   */
  private function record_needs_id( $record )
  {
    $saved_record = Participants_Db::get_participant( $record['id'] );
    $field_def = Participants_Db::$fields[$this->id_field];
    
    return ! isset( $saved_record[$this->id_field] ) || empty( $saved_record[$this->id_field] ) || $saved_record[$this->id_field] === $field_def->default_value();
  }
  
  /**
   * saves the member ID to the database
   * 
   * @global wpdb $spdb
   * 
   * @param int $record_id
   * @param string $id the member ID to save
   */
  private function store_member_id( $record_id, $id )
  {
    global $wpdb;
    $wpdb->update( Participants_Db::$participants_table, array( $this->id_field => $id ), array( 'id' => $record_id ) );
  }
  
  

  /**
   * generates the new ID
   * 
   * 
   * @return string the new member ID
   */
  private function generate_new_member_id()
  {

    $last_id = $this->last_saved_member_id();

    if ( !$last_id ) {
      // no ids have been saved yet, so we start here
      $last_id = $this->start_value;
    }

    /*
     *  we start the process with the previous ID
     */
    $new_id = $last_id;

    /*
     * this will keep looping as long as the ID we are trying is not unique
     * 
     * as soon as we try a unique one, it will break out of the loop
     */
    while ( !$this->member_id_is_unique( $new_id ) ) {

      /*
       * this is where we generate the new ID
       * 
       * you could make up your own formula here, for this example we simply increment the value
       */
      $new_id = $new_id + 1;
    }
    
    return $new_id;
  }

  /**
   * provides the last entered ID
   * 
   * @global wpdb $wpdb
   */
  private function last_saved_member_id()
  {
    global $wpdb;
    $sql = 'SELECT `' . $this->id_field . '` FROM ' . Participants_Db::$participants_table . ' ORDER BY `date_recorded` DESC LIMIT 1';
    return $wpdb->get_var( $sql );
  }

  /**
   * checks for a duplicate ID
   * 
   * @global wodb $wpdb
   * @param string $member_id the id value
   * @return bool true if the value is unique
   */
  private function member_id_is_unique( $member_id )
  {
    global $wpdb;
    $sql = 'SELECT `id` FROM ' . Participants_Db::$participants_table . ' WHERE ' . $this->id_field . ' = "%s"';
    $result = $wpdb->get_col( $wpdb->prepare( $sql, $member_id ) );
    return $wpdb->num_rows === 0;
  }

}

new pdb_generate_member_id(); // instantiates the class

What I’m after is a member_id that is comprised of the first 4 characters from the last_name field and then add the 4-digit code to that. Apparently it’s easy to do, but the developer won’t do it for me. He did however offer this advice:

When the maybe_save_member_id method is called by the filter, the contents of the record is passed in. You can pass that info along to the generate_new_member_id method so you can use that info to construct your ID.

Any help really, really appreciated. Thanks, Doug.

The following is probably a stupid question. Why don’t you just use the user’s id that is in the MySQL Database Table as the Member ID - I am sure it’s a unique number?

I could indeed - however I’m being asked to use this particular format so that it’s not just numbers that make up the ID.

What if the last name is “Xu” or “Bly”? … uh oh

Sponsor our Newsletter | Privacy Policy | Terms of Service