scottstarl
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitSorry about that, I might seem incompetent because I had zero experience with any coding including html prior to starting my 2 websites a month ago… I retyped the code last time that I pasted it here and had some typos.
I have had some progress in the last day, everything is working properly now, except for still receiving duplicate emails. I have put a random number generator in the subject line and observed that it is outputting different numbers each time, so the code is being repeated somehow. I have moved the remove_action command to different places in the snippet and had no luck yet. I even tried pasting it into every single bracket and it didn’t do anything. This is the latest version that is only having the final issue I mentioned. Again, any help again is appreciated!
// Loop to start add_action( 'application_notification' ); // Pimary function to run inside loop function application_notification() { // Grabs the currents pages id $postid = get_the_ID(); // Check if page id is correct, then run the commands if so if( $postid = 1388 ) { // Check if cookie is set or otherwise if user has visited this page before, then run commands if so if( isset( $_COOKIE['first_app_time'] ) ) { // Email and cookie constants // Headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Where the email is sent, seperated by commas $to ="email, email"; // Time of visit $current_date_time = current_datetime()->format('m/d/Y g:i a'); // Time of most recent visit that was stored in the cookie $last_app_page_visit = $_COOKIE['first_app_time']; // How long the cookie lasts, the number is the amount of days $expiration_time = 365 * DAY_IN_SECONDS; // Grabs the data for the user currently viewing the page $user = wp_get_current_user(); // Full name of the user $display_name = $user->display_name; // Email of the user $user_email = $user->user_email; // All of these are added together to create the email subject, use .$subject for subsequent lines, values need their own line $subject = "Application Viewed Again By: " . rand(0,1000000); $subject .= "$display_name"; // All of these are added together to create the email body, use .$msg for subsequent lines, values need their own line $msg = 'name & name,<br/><br/>'; $msg .= "$display_name"; $msg .= " ("; $msg .= "$user_email"; $msg .= ") "; $msg .= "just opened the page to begin their application again.<br/><br/>The last time they viewed it was on: " ; $msg .= "$last_app_page_visit"; $msg .= '</br><br/>The time is currently: '; $msg .= "$current_date_time"; // Send email wp_mail( $to, $subject, $msg, $headers ); // Remove cookie so we can reset the timestamp unset( $_COOKIE['first_app_time'] ); // Set the cookie with the current time setcookie( 'first_app_time', $current_date_time, time() + $expiration_time ); // If cookie is not set or otherwise the user has not visited this page before, but the page number is correct, run these commands instead } else { // Email and cookie constants // Headers $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Where the email is sent, seperated by commas $to ="email, email"; // Time of visit $current_date_time = current_datetime()->format('m/d/Y g:i a'); // How long the cookie lasts, the number is the amount of days $expiration_time = 365 * DAY_IN_SECONDS; // Grabs the data for the user currently viewing the page $user = wp_get_current_user(); // Full name of the user $display_name = $user->display_name; // Email of the user $user_email = $user->user_email; // All of these are added together to create the email subject, use .$subject for subsequent lines, values need their own line $subject = "name First Time View: " . rand(0,1000000); $subject .= "$display_name"; // All of these are added together to create the email body, use .$msg for subsequent lines, values need their own line $msg = 'name & name,<br/><br/>'; $msg .= "$display_name"; $msg .= " ("; $msg .= "$user_email"; $msg .= ") "; $msg .= "just opened the page to begin their application for the first time!" ; $msg .= '</br><br/>The time is currently: '; $msg .= "$current_date_time"; // Send email wp_mail( $to, $subject, $msg, $headers ); // Set the cookie with the current time setcookie( 'first_app_time', $current_date_time, time() + $expiration_time ); } } // Stops action only if it has run, this prevents looping and will only allow it to run again when the page is loaded again remove_action( 'application_notification' ); }and here’s the condensed snippet without my notes, the paragraph blocks here don’t save the indention of the lines so it’s less organized:
add_action( 'application_notification' );
function application_notification() {
$postid = get_the_ID();
if( $postid = 1388 ) {
if( isset( $_COOKIE['first_app_time'] ) ) {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to ="email, email";
$current_date_time = current_datetime()->format('m/d/Y g:i a');
$last_app_page_visit = $_COOKIE['first_app_time'];
$expiration_time = 365 * DAY_IN_SECONDS;
$user = wp_get_current_user();
$display_name = $user->display_name;
$user_email = $user->user_email;
$subject = "Application Viewed Again By: " . rand(0,1000000);
$subject .= "$display_name";
$msg = 'name & name,
';
$msg .= "$display_name";
$msg .= " (";
$msg .= "$user_email";
$msg .= ") ";
$msg .= "just opened the page to begin their application again.
The last time they viewed it was on: " ;
$msg .= "$last_app_page_visit";
$msg .= '
The time is currently: ';
$msg .= "$current_date_time";
wp_mail( $to, $subject, $msg, $headers );
unset( $_COOKIE['first_app_time'] );
setcookie( 'first_app_time', $current_date_time, time() + $expiration_time );
} else {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to ="email, email";
$current_date_time = current_datetime()->format('m/d/Y g:i a');
$expiration_time = 365 * DAY_IN_SECONDS;
$user = wp_get_current_user();
$display_name = $user->display_name;
$user_email = $user->user_email;
$subject = "name First Time View: " . rand(0,1000000);
$subject .= "$display_name";
$msg = 'name & name,
';
$msg .= "$display_name";
$msg .= " (";
$msg .= "$user_email";
$msg .= ") ";
$msg .= "just opened the page to begin their application for the first time!" ;
$msg .= '
The time is currently: ';
$msg .= "$current_date_time";
wp_mail( $to, $subject, $msg, $headers );
setcookie( 'first_app_time', $current_date_time, time() + $expiration_time );
}
}
remove_action( 'application_notification' );
}Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitStill acting very buggy… I’ve been testing a bunch of different ways and this is where I’ve ended up. Sometimes it doesn’t grab the username at all, other times it doesn’t grab the time at all. It also still starts an infinite loop of sending emails that doesn’t stop unless I remove the snippet, everything else seems to be working normally though.
function application_notification() { $visit_time = date('m/d/Y g:i a'); $expiration_time = 365 * DAY_IN_SECONDS; if(is_page( 1388 )) { if(isset($_COOKIE['first_app_notification_visit_time'])) { $last_app_page_visit = $_COOKIE['first_app_notification_visit_time']; $user = wp_get_current_user(); $user_email = $user->user_email; $to ="private-email-here"; $subject = "First App Page Viewed Again"; $msg = 'NAME, '; $msg .= "$user_email"; $msg .= "just opened the page to begin their first application for the again. The last time they viewed it was on " ; $msg .= "$last_app_page_visit"; $msg .= 'The time is currently: '; $msg .= "$visit_time"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <private-email-here>' . "\r\n"; $headers .= 'CC: private-email-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); unset($_COOKIE['first_app_notification_visit_time']); setcookie('first_app_notification_visit_time', $visit_time, time() + $expiration_time, COOKIEPATH, COOKIE_DOMAIN); return wp_mail; ) else { $user = wp_get_current_user(); $user_email = $user->user_email; $to ="private-email-here"; $subject = "First App Page Viewed"; $msg = 'NAME, '; $msg .= "$user_email"; $msg .= "just opened the page on the training site to begin their first application for the first time! " ; $msg .= 'The time is currently: '; $msg .= "$visit_time"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <private-email-here>' . "\r\n"; $headers .= 'CC: private-email-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); setcookie('first_app_notification_visit_time', $visit_time, time() + $expiration_time, COOKIEPATH, COOKIE_DOMAIN); return wp_mail; ) else {} remove_action('init', 'application_notification'); } add_action('init', 'application_notification');Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitInteresting! Thanks for responding. So what you’re saying is that it should look like what I typed below? Or should it just be ended once at the end since the “else” function can only be activated if the first “if” is false?
function application_notification() { $visit_time = date('F j, Y g:i a'); $postid = get_the_ID(); if(!isset($_COOKIE['first_app_notification_visit_time']) && !$postid = 1388) { $last_app_page_visit = $_COOKIE['first_app_notification_visit_time']; $user = wp_get_current_user(); $user_email = $user->user_email; $to ="email-address-here"; $subject = "First App Page Viewed Again"; $msg = 'User and User, <br> <br>'; $msg .= "$user_email"; $msg .= "just opened the page to begin their first application for the again. The last time they viewed it was on <br> " ; $msg .= "$last_app_page_visit"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <email-address-here>' . "\r\n"; $headers .= 'CC: email-address-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); unset($_COOKIE['first_app_notification_visit_time']); setcookie('first_app_notification_visit_time', $visit_time, time()+31556926); } else ($postid = 1388) { $to ="email-address-here"; $subject = "First App Page Viewed"; $msg = 'User and User, <br> <br>'; $msg .= "$user_email"; $msg .= "just opened the page on the training site to begin their first application for the first time!" ; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <email address here>' . "\r\n"; $headers .= 'CC: email-address-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); setcookie('first_app_notification_visit_time', true, time()+31556926); } } } } add_action('init', 'application_notification'); function application_notification() { $visit_time = date('F j, Y g:i a'); $postid = get_the_ID(); if(!isset($_COOKIE['first_app_notification_visit_time']) && !$postid = 1388) { $last_app_page_visit = $_COOKIE['first_app_notification_visit_time']; $user = wp_get_current_user(); $user_email = $user->user_email; $to ="email-address-here"; $subject = "First App Page Viewed Again"; $msg = 'User and User, <br> <br>'; $msg .= "$user_email"; $msg .= "just opened the page to begin their first application for the again. The last time they viewed it was on <br> " ; $msg .= "$last_app_page_visit"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <email-address-here>' . "\r\n"; $headers .= 'CC: email-address-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); unset($_COOKIE['first_app_notification_visit_time']); setcookie('first_app_notification_visit_time', $visit_time, time()+31556926); remove_action('init', 'application_notification'); } else ($postid = 1388) { $to ="email-address-here"; $subject = "First App Page Viewed"; $msg = 'User and User, <br> <br>'; $msg .= "$user_email"; $msg .= "just opened the page on the training site to begin their first application for the first time!" ; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <email address here>' . "\r\n"; $headers .= 'CC: email-address-here' . "\r\n"; wp_mail($to, $subject, $msg, $headers); setcookie('first_app_notification_visit_time', true, time()+31556926); remove_action('init', 'application_notification'); } } } remove_action('init', 'application_notification'); } add_action('init', 'application_notification');