Hi.
I am trying to validate a loyalty card through CRED. Please take note of the following:
• I have a custom user field called "membership-number"
• I have a CRED form to update the user fields ID number 1910
• I have a pre-defined list of loyalty cards in my MYSQL database called wp_cards
• The columns of the above table in the database are: ID, membership_number, barcode, card_holder, starter_pack
What I am trying to achieve:
I need to validate the user's membership card against the pre-defined database table to ensure that the card has been physically issued. I need to run a database query to query the data before passing errors.
I have done the database query on a page template to check if it is possible and it runds no problem. However, if I add it into the add_filter('cred_form_validate','my_validation',10,2); filter it does not work (I get a blank screen once submitted). If I take the database query out it works. So my question is, Is it possible to do a database query inside the validation filter? And if not, how would we achieve this?
Here is the code on the page template successfully querying the data:
$user_id = get_current_user_id();
$key = 'wpcf-membership-number';
$single = true;
$my_number = get_user_meta( $user_id, $key, $single );
$mylink = $wpdb->get_row( "SELECT * FROM wp_cards WHERE membership_number = $my_number ", ARRAY_N );
if ($mylink == NULL)
{
echo '<h1>No membership number!</h1>';
} else {
echo '<h1>My membership card details are:</h1>';
echo '<p>ID: ' . $mylink[0] . '<br/>'; // prints "10"
echo 'Membership Number: ' . $mylink[1] . '<br/>'; // prints "10"
echo 'Barcode: ' . $mylink[2] . '<br/>'; // prints "10"
echo 'Card Holder: ' . $mylink[3] . '<br/>'; // prints "10"
echo 'Starter Pack: ' . $mylink[4] . '</p>'; // prints "10"
if($mylink[3] == 0)
{
echo '<p>Good news! <span style="font-size:30px; font-weight:800;">Card not assigned</span></p>';
} else{
if($mylink[3] !== 0)
{
$user_id = $mylink[3];
$key = 'first_name';
$single = true;
$card_holder = get_user_meta( $user_id, $key, $single );
echo '<p>Card already assigned to <span style="font-size:30px; font-weight:800;">' . $card_holder . '</span></p>';
}
}
}
Here is the code for the CRED form validation with the broken database query:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
list($fields,$errors)=$field_data;
//print_r($fields);
if ($form_data['id']==1910)
{
$my_number = $fields['wpcf-membership-number']['value'];
$mylink = $wpdb->get_row( "SELECT * FROM wp_cards WHERE membership_number = $my_number ", ARRAY_N );
if ($mylink == NULL)
{
$errors['wpcf-membership-number']='Invalid membership number';
}
}
return array($fields,$errors);
}
****I will not be able to provide you with FTP details should the need arise as this is a client based website and we need to ensure the safety of our clients details ****