Sep 232014

I found a small problem with the way I was generating the membership number, the solution I documented on this post works BUT if a member registers but then fails to activate their membership before another person registers they both have the same membership number and user name. WordPress, rightly, does not allow two members to have the same user name.

I therefore created a custom table in the WordPress database called membership. It has two fields ID and member, ID is set to 1 and the member field is set to one less than whatever you want the starting membership number to be. The code in functions.php therefore needs to be changed to read and write the membership number to the custom table. Rather than use a custom table I could have used the options table but I thought it better to have the custom table so that it is easier to edit with phpmyadmin if necessary. The code now looks like:

add_action('gform_pre_validation', 'pre_validation_handler');
function pre_validation_handler($form){
	if ( 2 == $form['id'] ) {
		global $wpdb;
        $myrows = $wpdb->get_row("SELECT * FROM membership WHERE ID = 1" );
        $member = $myrows->member;
        $member ++;
        $wpdb->update('membership',array('member' => $member),array('ID'=> 1));
        $_POST["input_5"] = $member;
// have to return the form either way
	return $form;

This now works correctly but it does mean there may be gaps in membership numbers should members who register not activate their accounts.

 Posted by at 5:59 pm