Confirming record with Email Link
-
A new member signs up and receives an email with record link.
The member clicks on the record link in the email.
The links takes the member to the individual record. (“pid=AAAAA”)If one wants to SET a field (confirmed=”yes”), is this the best way?
What alternatives exist?
where should the code be inserted?
Thanks!<?php
$link = mysqli_connect(“server”, “user”, “passwd”, “wp_participants_database”);
if($link === false){
die();
}
if(!empty($_GET[‘pid’])) {
$Rec_id = _GET[‘pid’];
} else{
$Rec_id = ‘0’;
}
$sql1 = “SELECT confirmed from participants_database WHERE record_link=$Rec_id;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE record_link=$Rec_id;
if (mysqli_query($link, $sql1) !== ‘yes’) {
$result = mysqli_query($link, $sql2));
} else {
die();
}
mysqli_close($link);
?>
-
Each record has a field named ‘last_accessed’ that will be null until the record is accessed using the frontend record edit. That will give you a timestamp for when the record was last opened by the user. You can use that to determine if the record has been confirmed or not.
If you do want to set a special field value, you would use the ‘pdb-before_submit_update’ filter which happens when a record update is getting updated. You can use that to set your field value by simply setting it in the post array via the filter. You’ll want to check that the update is happening on the frontend, since the filter is used for both frontend and backend record updates.
I’ll check it out. Thanks!
Tried to follow the guides to create a plugin. Code below.
Using “self::” gets white screen;
syntax check gets a php7 error “Cannot use “self” when no class scope is active in your code”Using php5 syntax check gets error “Using $this when not in object context”
In either event plugin does not work. It does not update the field ‘confirmed’.Suggestions? What am I missing?
My attempted plugin contribution:
==========================================================
<?php
/* Plugin Name: Participants Database Confirm Record by Email
Description: Auto set confirmed = ‘yes’ when their record is first accessed by user. */add_filter( ‘pdb-before_submit_update’, ‘pdb_set_record_confirmed’ );
/**
* @param array $post the submitted record data
* @return array the new record data
*/
function pdb_set_record_confirmed( $recrd_id )
{
if ( is_admin() ) {
/** if we are the admin do nothing and return */
return;
}
/* ACTION FOR SET RECORD CONFIRMED: */$recrd_id = false;
// get the pid from the get string if given
$get_pid = filter_input( INPUT_GET, Participants_Db::$record_query, FILTER_SANITIZE_STRING );if ( empty( $get_pid ) ) {
$get_pid = filter_input( INPUT_POST, Participants_Db::$record_query, FILTER_SANITIZE_STRING );
}
if ( !empty( $get_pid ) ) {
$recrd_id = self::get_participant_id( $get_pid );
}
if ( $recrd_id === false ) {
$recrd_id = self::$session->record_id( true );
}global $wpdb;
$table1 = “wp_participants_database”;
$sql1 = “SELECT confirmed FROM $table1 WHERE id=$recrd_id”;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE id=$recrd_id;”;$confirm = $wpdb->get_var($sql1);
if ( $confirm !== “yes” ) {
$result = $wpdb->update(
$table1,
array(
‘confirmed’ => ‘yes’ // string
),
array( ‘id’ => $recrd_id ));
} else {
$result = “Already set”;
}
return $result; /* * return what? */
}
==============================================================There are several errors in the code (you’re probably not surprised to hear that!)…but there are two very important things you should understand about using that filter.
First, the function gets the posted data array as its argument. There is no need to try to find the record id, it’s in the post array as $post[‘id’]
That will eliminate most of the first part of your function.
Second, the function must return the $post array or the record will fail to update. So that solves you dilemma at the end as to what to return. If the record is already confirmed, you do nothing. You’re going to return the $post array in all cases.
Be patient! My first time in PHP beyond “Hello, World”. Thanks very much for the advice.
The code below seems to work, but it does not update when the record is viewed, but ONLY if the record viewer clicks the update button.
Can it be made to confirm when viewed via email link? Thanks again!My revised plugin code:
================================================================
<?php
/* Plugin Name: Participants Database Confirm When viewed Email link
Description: Auto set confirmed = ‘yes’ when record is accessed by user. */add_filter( ‘pdb-before_submit_update’, ‘pdb_set_record_confirmed’ );
/**
* @param array $post the submitted record data
* @return array the new record data
*/
function pdb_set_record_confirmed( $post )
{
if ( is_admin() ) {
/** if we are the admin do nothing and return */
return;
}
/* ACTION FOR SET RECORD CONFIRMED: */$recrd_id = $post[‘id’];
global $wpdb;
$table1 = “wp_participants_database”;
$sql1 = “SELECT confirmed FROM $table1 WHERE id=$recrd_id”;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE id=$recrd_id;”;$confirm = $wpdb->get_var($sql1);
if ( $confirm !== “yes” ) {
$result = $wpdb->update(
$table1,
array(
‘confirmed’ => ‘yes’ // string
),
array( ‘id’ => $recrd_id ));
}
return $post; /* * return updated array */
}
===============================================================That would have to work in a completely different way. One way to do this is to create a custom template that has javascript that would update the record when it is viewed.
Another possibility is the use the ‘pdb-shortcode_call_pdb_record’ filter to record the view. This filter gets the array of shortcode attributes before the shortcode is displayed. You’d have to look at the requested URL to find the private ID, then get the record ID from that.
All that work for nothin’. Shucks.
Can you translate this:
“use the ‘pdb-shortcode_call_pdb_record’ filter to record the view”
into English? (-;
What do you mean by “record the view”?So if I get the attributes and record ID, how do I set confirmed = “yes”? Sorry, I’m not getting it.
It’s not too different from what you were doing before, you just need to get the record ID in a different way. When the ‘pdb-shortcode_call_pdb_record’ filter is called, you will need to find out the record ID from the URL. If the private ID is in the URL, you can get that with $_GET[‘pid’] and then from there use Participants_Db::get_participant_id($_GET[‘pid’]) to get the current record ID, then you can update the record. Be sure to return the value that the filter function gets, like you were doing before.
Ok, so I have attempted to update the record when a non-admin user views the record. Below is the code I cam up with. No errors reported, but it does not set the variable. Any hint what might be wrong?
Here’s the attempt at the plugin:
================================================================
<?php
/* Plugin Name: Participants Database Set Variable upon View by Non-admin user.
Description: Auto set confirmed=’yes’ when record viewed by user. */
add_filter( ‘pdb-shortcode_call_pdb_record’, ‘pdb_set_record_confirmd’ );
/**
* @param array $post the submitted record data
* @return array the new record data
*/
function pdb_set_record_confirmd( $poste )
{
if ( is_admin() ) {
/** if we are the admin do nothing and return */
return $poste;
}
/* ACTION FOR SET RECORD CONFIRMED: */
$pid_in = $_GET[‘pid’];
$rec_id = false;
// get the record id from the get pid if given
$rec_id = filter_input( INPUT_GET, Participants_Db::get_participant_id(‘$pid_in’), FILTER_SANITIZE_STRING );
if ( empty( $rec_id ) ) {
return $poste;
}
if ( $rec_id === false ) {
return $poste;
}
global $wpdb;
$table1 = “wp_participants_database”;
$sql1 = “SELECT confirmed FROM $table1 WHERE id=$rec_id”;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE id=$rec_id;”;
$confirm = $wpdb->get_var( $sql1 );
if ( $confirm !== “yes” ) {
$result = $wpdb->update(
$table1,
array( ‘confirmed’ => ‘yes’ ),
array( ‘id’ => $rec_id )
);
}
return $poste; /* * return updated record */
} // end of function
?>
===============================================================================I suggest you debug your code by adding a debug statements to test your queries and the results to make sure you are getting what you expect to get.
Something like this will tell you what the query was used…put this one after the first wpdb call:
<?php error_log( ' get var: '.$wpdb->last_query.' result: ' . $confirm); ?>
then this one just before the return:
<?php error_log( ' query: ' . $wpdb->last_query ); ?>
this assumes you have your php error log set up. You won’t need the php tags from those examples, it’s just there to tell you that it is php code.
pid_in is correct, but neither of these lines work:
$rec_id = filter_input( INPUT_GET, Participants_Db::get_participant_id(‘$pid_in’), FILTER_SANITIZE_STRING );
or in the alternative:
$rec_id = Participants_Db::get_participant_id(‘$pid_in’);
Not sure how to diagnose this one…
Any suggestions?
I’m sorry, I really can’t debug your code for you. I don’t see anything wrong, but I honestly don’t understand what you’re trying to do here.
You’ll just have to figure this out.
I understand. This stuff is complicated. I wanted to have the user record marked “confirmed” when the member used the email link to VIEW his own record. You led me to create a plugin that marks “confirmed” when the user UPDATES the record. On further review I have created the plugin that sets “confirmed” to “yes” just because the user viewed the record. I have now figured this out.
Thanks for the system and the advice! Yours is a tremendous contribution to WordPress. Here are my two (2) corrected plugins:
Confirm on UPDATE
============================================================
<?php
/* Plugin Name: Participants Database Confirm on Non-admin Update
Description: Auto set confirmed=’yes’ when record is updated by user. */add_filter( ‘pdb-before_submit_update’, ‘pdb_set_record_confirmed’ );
/**
* @param array $post the submitted record data
* @return array the new record data
*/
function pdb_set_record_confirmed( $post )
{
if ( is_admin() ) {
/** if we are the admin do nothing and return */
return $post;
}
/* ACTION FOR SET RECORD CONFIRMED: */$recrd_id = $post[‘id’];
global $wpdb;
$table1 = “wp_participants_database”;
$sql1 = “SELECT confirmed FROM $table1 WHERE id=$recrd_id”;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE id=$recrd_id;”;$confirm = $wpdb->get_var($sql1);
if ( $confirm !== “yes” ) {
$result = $wpdb->update(
$table1,
array(
‘confirmed’ => ‘yes’ // string
),
array( ‘id’ => $recrd_id ));
}
return $post; /* * return updated array */
} / end of function
?>
===============================================================Confirm when Non-Admin User VIEWS Record:
=============================================================
<?php
/* Plugin Name: Participants Database Set Variable upon View by Non-admin user.
Description: Auto set confirmed=’yes’ when record viewed by user. */add_filter( ‘pdb-shortcode_call_pdb_record’, ‘pdb_set_record_confirmd’ );
/**
* @param array $post the submitted record data
* @return array the new record data
*/
function pdb_set_record_confirmd( $poste )
{
if ( is_admin() ) {
/** if we are the admin do nothing and return */
return $poste;
}
/* ACTION FOR SET RECORD CONFIRMED: */
$record_id = false;
// get the pid from the get string if given (for backwards compatibility)
$get_pid = filter_input(INPUT_GET, ‘pid’, FILTER_SANITIZE_STRING);
if (empty($get_pid)) {
$get_pid = filter_input(INPUT_POST, ‘pid’, FILTER_SANITIZE_STRING);
}
if (!empty($get_pid)) {
$record_id = Participants_Db::get_participant_id($get_pid);
}
if ( empty( $record_id ) ) {
return $poste;
}
if ( $record_id === false ) {
return $poste;
}global $wpdb;
$table1 = “wp_participants_database”;
$sql1 = “SELECT confirmed FROM $table1 WHERE id=$record_id”;
$sql2 = “UPDATE participants_database SET confirmed=’yes’ WHERE id=$record_id;”;
$confirm = $wpdb->get_var( $sql1 );if ( $confirm !== “yes” ) {
$result = $wpdb->update(
$table1,
array( ‘confirmed’ => ‘yes’ ),
array( ‘id’ => $record_id )
);
}return $poste; /* * return updated record */
} // end of function
?>
================================================================Hope there aren’t any bugs!
- The topic ‘Confirming record with Email Link’ is closed to new replies.