scottstarl
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitI can create an ID for the <div> with HTML anchor , but not for the <a class> itself. I’ve tried using the <div> ID I created but it doesn’t do anything.
I hope you have a Happy Thanksgiving! Will be a nice break from this headache lol
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitSo just an update – still haven’t got it fixed. I’ve resorted to trying to use a js event listener on the buttons that lead to the page (regular wordpress buttons). A work in progress, but no luck getting the js to exectute on click… This forum deletes scripts so I just replaced the tags with “pretend this is a script tag”
function jit_js() { if(is_user_logged_in()) { $postid = get_the_ID(); if($postid = 2049) { if(! is_admin()) { if(! current_user_can('edit_posts')) { $current_date_time = current_datetime()->format('m/d/Y g:i a'); $set_expiration_time = time() + (365 * DAY_IN_SECONDS); $remove_expiration_time = time() - (365 * DAY_IN_SECONDS); $user = wp_get_current_user(); $display_name = $user->display_name; $user_email = $user->user_email; if(empty( $_COOKIE['jit_app_time'])) { $subject = 'J.I.T. First Time View: '; $subject .= "$display_name"; $body = 'NAME and NAME, '; $body .= "$display_name"; $body .= ' ('; $body .= "$user_email"; $body .= ') '; $body .= 'just opened the page to begin their J.I.T. application for the first time!'; $body .= ' The time is currently: '; $body .= "$current_date_time"; } elseif(! empty( $_COOKIE['jit_app_time'])) { $last_app_page_visit = $_COOKIE['jit_app_time']; $subject = 'J.I.T. Viewed Again By: '; $subject .= "$display_name"; $body = 'NAME and NAME, '; $body .= "$display_name"; $body .= ' ('; $body .= "$user_email"; $body .= ') '; $body .= 'just opened the page to begin their J.I.T. application again. The last time they viewed it was on: '; $body .= "$last_app_page_visit"; $body .= ' The time is currently: '; $body .= "$current_date_time"; } ?> <pretend this is a script tag> const jit = document.getElementById("jit-id"); const jit_notification = function() { $jit_url = "https://domain.com/?page_id=1388" $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Training Site <updates@domain.com>' . "\r\n"; $to = "name@domain.com, name@domain.com"; wp_mail( $to, $subject, $body, $headers ); unset($_COOKIE['jit_app_time']); setcookie('jit_app_time',$current_date_time,$remove_expiration_time); setcookie('jit_app_time',$current_date_time,$set_expiration_time); wp_redirect( $jit_url ); exit; </pretend this is a script tag> <?php } } } } } add_action('wp_footer', 'jit_js');Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitOh and also the buttons I was using to open the link were indeed page ids not urls… so that caused another redirect. For todays tests I just kept refreshing the page (I’ll fix the buttons later, that was just my way of permalinking them in case I wanted to change the slugs again and not have to replace all of the links)
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitWell there was a “user experience program” that was forcing api calls via XMLHttpRequest that I found… shut it off immediately and blocked XML requests altogether. Then I added: if( ! current_user_can( ‘edit_posts’ ) ) and changed the action to: add_action( ‘wp_loaded’, ‘application_notification’, 999999 );
It significantly reduced the emails being sent. Now I’m down to it sending two emails every time! Definitely major progress, but that second one is going to drive me insane. I didn’t have the time yet to try making my own plugin yet or researching the defined() check yet, so that’s why I used wp_loaded in. I’ll keep messing with it using your advice tomorrow and see what happens. Thanks again for all of your help so far… so close!
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitSorry I got really busy with work. Is there somewhere you could direct me to learn how you’re inserting the code to your site? I want to test that and see if maybe the fact that I was using a plugin to do it might have been the issue!
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitMy LMS blocks access by creating a new role labeled member (no access to anything) , then child roles that are the memberships themselves (each membership grants access to a new course, so you collect more and more memberships along the way as you progress. The memberships are granted manually since they’re free / for employees). I’m able to set which membership can access which page IDs. It’s a paid plugin called ARMember (https://www.armemberplugin.com/)
Without being logged in, you can’t be a member
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitI did add the logged in action yesterday that you mentioned and it didn’t seem to fix it, my next guess would be that it possibly was being caused by CDN creating issues with cookies specifically and same issue with caching ,I read that quite a few other people were having that issue before. Scratching my head trying to figure out what the issue is still… I know that it can’t be bot activity because I am going to that page using another account that I created in a different window. The emails I’m getting reference the correct user (that account I’m using to load the page), it’s really just an issue of multiple emails. Is there a way to do this after page load and still have it work properly? I’m wondering if it’s neccesary to have it done during page load, or if I can have it add_action after maybe it loads a specific block or specific image. If there is something else I can send you that would help give clues to what the issue might be I am more than happy to send it! I’m not sure if there is a log that I can generate that could be read through to make whatever the issue here is obvious, but that sounds like it would be the right direction…
The only change in the latest version was just wrapping get_post_id and it’s contents with is_user_logged_in, then wrapping that with “if ( ! is_admin () )”
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitI just read about something that may be leading to issues, do you think CDN or cache plugins can cause issues? This plugin is a “must be installed” one: https://objectcache.pro/
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page Visit“In real world usage, how many notifications are you expecting? Unless it’s just a few, wouldn’t it make more sense to simply log the visits? Of course the normal server access log does this anyway, but it doesn’t discriminate. It will not indicate first time or return, nor WP user data. A bespoke logging scheme would make sense if the email volume gets to be too much.”
Since this is a private site for training employees, it’s crucial to understand when they hit a certain checkpoint so they can be reached out to. Relying on leaving a “reach out to so and so” message really just doesn’t do the trick.
I implemented the changes, no luck though. Still many emails, let me ensure that I didn’t make a mistake. I will dig into this again at the end of the when I have some time!
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitCan’t be bot activity, since I’m using this website as an LMS everything requires the correct membership to access, which they aquire via admin approval! I will still add the is_user_logged_in() check as another precaution to avoid future bugs.
Thank you for the insight, I had no idea that ! would negate something…. I was using it in CSS style in a frowned upon way last week:”!important”. I 100% misunderstood how it works in PHP!
I feel very optimistic about using the changes you suggested. Will test today and report back with results!
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitOh and the backlog of emails I referenced, they’re not pretyped, its a log of code. I know that because it will send blank values where $user should be since I’m not on the page anymore. That leads me to believe it is running when I’m not on the page rather than simply sending emails late that were already generated. If I just let the code run for an hour, it eventually stops sending emails, then reactivating it will start dumping the backlog…
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitFinal version before bed, still having the same issue. Removed all plugins that relate to email, didn’t fix it. Installed a plugin so that the office 365 uses OAth and that didn’t seem to fix it either. Maybe an issue with cron? Not too sure… I tried using a sleep function to force a pause and allow the code to catch up (similar to how I would use AHK), but that didn’t seem to work.
Something that I noticed that may be a clue to what’s going on: When I deactivate the snippet, then reactivate it later, even if it’s been an hour, the server will start sending old emails. It seems as if it’s creating a backlog, then when I use the code again it picks up where it left off, then proceeds to start packing more wp_mail into the log. I’ve been using code snippets (https://wordpress.org/plugins/code-snippets/) to put this into the functions.php, now I’m wondering if just the add_action should be there and the rest of the code should be somewhere else. The lack of experience really just forces you to hit a wall of forums and 1000 google searches.
Here was the last version I had, still just the same issue of many duplicates (everything else I’m trying to do here works perfectly though). I only replaced the emails with “email” and names with “Name” for privacy reasons, everything else is the same. This last time I set the plugin to “Only run on site front-end” and left the priorty default at “10”. Didn’t notice any difference there. I want to agree with you on the race condition after researching what that is, but considering the backlog, it’s too sporadic with how many it sends for me to understand:
// Begin notification loop - CHANGE NOTIFICATION NAME FOR NEW ITERATION add_action( 'init', 'application_notification' ); // Actions for notification loop - CHANGE NOTIFICATION NAME TO MATCH LOOP FOR NEW ITERATION function application_notification() { // Prevents additional looping without stopping current loop - CHANGE NOTIFICATION NAME FOR NEW ITERATION remove_action( 'init', 'application_notification' ); // Grabs the currents pages id - DO NOT CHANGE FOR NEW ITERATION $postid = get_the_ID(); // Do this if page id is correct - CHANGE PAGE NUMBER FOR NEW ITERATION if( $postid = 1388 ) { // Do this instead if cookie is not set - CHANGE COOKIE NAME FOR NEW ITERATION if( !empty( $_COOKIE['jit_app_time'] ) ) { // Constant settings for all cases // Date and time // Time of visit - DO NOT CHANGE FOR NEW ITERATION $current_date_time = current_datetime()->format('m/d/Y g:i a'); // Cookie settings // How long the cookie lasts, the number is the amount of days - CHANGE LENGTH OF COOKIE IN DAYS FOR NEW ITERATION $expiration_time = 365 * DAY_IN_SECONDS; // User data // Grabs the data for the user currently viewing the page - DO NOT CHANGE FOR NEW ITERATION $user = wp_get_current_user(); // Full name of the user - DO NOT CHANGE FOR NEW ITERATION $display_name = $user->display_name; // Email of the user - DO NOT CHANGE FOR NEW ITERATION $user_email = $user->user_email; // Email settings // Where the email is sent, seperated by commas - CHANGE EMAIL ADDRESSES FOR NEW ITERATION $to = "email, email"; // Specific settings for this if only // Email settings // All are added together, use $subject .= for subsequent lines, values need their own line - CHANGE EMAIL SUBJECT FOR NEW ITERATION $subject = 'J.I.T. First Time View: '; $subject .= "$display_name"; // All are added together, use $body .= for subsequent lines, values need their own line - CHANGE EMAIL BODY FOR NEW ITERATION $body = 'Name and Name, '; $body .= "$display_name"; $body .= ' ('; $body .= "$user_email"; $body .= ') '; $body .= 'just opened the page to begin their J.I.T. application for the first time!'; $body .= ' The time is currently: '; $body .= "$current_date_time"; // Send email - DO NOT CHANGE FOR NEW ITERATION wp_mail( $to, $subject, $body ); } // Do this if cookie is set - CHANGE COOKIE NAME FOR NEW ITERATION elseif( !isset( $_COOKIE['jit_app_time'] ) ) { // Constant settings for all cases // Date and time // Time of visit - DO NOT CHANGE FOR NEW ITERATION $current_date_time = current_datetime()->format('m/d/Y g:i a'); // Cookie settings // How long the cookie lasts, the number is the amount of days - CHANGE LENGTH OF COOKIE IN DAYS FOR NEW ITERATION $expiration_time = 365 * DAY_IN_SECONDS; // User data // Grabs the data for the user currently viewing the page - DO NOT CHANGE FOR NEW ITERATION $user = wp_get_current_user(); // Full name of the user - DO NOT CHANGE FOR NEW ITERATION $display_name = $user->display_name; // Email of the user - DO NOT CHANGE FOR NEW ITERATION $user_email = $user->user_email; // Email settings // Headers - DO NOT CHANGE FOR NEW ITERATION // Where the email is sent, seperated by commas - CHANGE EMAIL ADDRESSES FOR NEW ITERATION $to = "email, email"; // Specific settings used when the last if is false only // Cookie settings // Timestamp stored in the most recent cookie - CHANGE COOKIE NAME FOR NEW ITERATION $last_app_page_visit = $_COOKIE['jit_app_time']; // Email settings // All are added together, use $subject .= for subsequent lines, values need their own line - CHANGE EMAIL SUBJECT FOR NEW ITERATION $subject = 'J.I.T. Viewed Again By: '; $subject .= "$display_name"; // All are added together, use $body .= for subsequent lines, values need their own line - CHANGE EMAIL BODY FOR NEW ITERATION $body = 'Name and Name, '; $body .= "$display_name"; $body .= ' ('; $body .= "$user_email"; $body .= ') '; $body .= 'just opened the page to begin their J.I.T. application again. The last time they viewed it was on: '; $body .= "$last_app_page_visit"; $body .= ' The time is currently: '; $body .= "$current_date_time"; // Send email - DO NOT CHANGE FOR NEW ITERATION wp_mail( $to, $subject, $body ); // Update cookie settings // Force cookie to expire so browser will remove it - CHANGE COOKIE NAME FOR NEW ITERATION setcookie( 'jit_app_time', $current_date_time, time() + $expiration_time ); // Remove cookie timestamp as back so we can reset it - CHANGE COOKIE NAME FOR NEW ITERATION unset( $_COOKIE['jit_app_time'] ); } // Retires and other arguments by doing nothing - DO NOT CHANGE FOR NEW ITERATIONS else{} // Set the cookie with the current time - CHANGE COOKIE NAME FOR NEW ITERATION setcookie( 'jit_app_time', $current_date_time, time() + $expiration_time ); } }Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitAlso, will this forum redact personal email addresses when I post exact code?
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitThank you for your patience, I’m going to crack this today. I am using SMTP for my emails by the way, it’s configured through the free version of the plugin: Admin and Site Enhancements (ASE). That was why I used the random number generator, to make sure it wasn’t my other website’s SMTP server causing the duplicates! The SMTP does not go through the plugins servers, it’s using a secondary website I own that I already had email addresses set up for using Office 365.
I believe I can get this fixed today with what you’ve given me. I will update you in a bit when I have some more progress… I’ve been resorting to trying to have the php function activate when the button that opens that page is clicked instead since that’s the only way to navigate to the page… If that doesn’t work out I’m going to come back to having the function activate automatically then detect the page ID and see if it works.
I will say though, it seems that the previous iteration of this code I was working on would fire as many emails as it could in about two minutes, then stop unless I started scrolling or otherwise having activity on the page that was referenced in the if command. I appreciate your continued help!
Forum: Developing with WordPress
In reply to: Emailing Logged In Users Upon Page VisitAlso I noticed it converts my spacing in the subject and email lines using < br / > to actual spaces when I paste it here, so that’s why it looks slightly off…