php - WP Cron Job Runs On Every Page Load -


i trying wrap head around wp cron jobs , have been playing around following snippet of code, supposed send email specified email address once hour, provided of course visitor has visited site , triggered job (wp cron jobs aren't apparently real chron jobs, i've come understand).

now, job works in email gets sent email address, there 2 problems:

  1. the email gets sent multiple times per chron job
  2. cron jobs seem triggered on every page load though it's not supposed run more once hour

here's code (retrived wp_schedule_event not firing):

if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) {     wp_schedule_event( time(), 'hourly', 'prefixhourlyevent');        }  add_action( 'prefixhourlyevent', 'prefix_do_this_hourly' );  function prefix_do_this_hourly() {           wp_mail('myemail@gmail.com','cron working', 'cron working: ','',''); } 

would able give me idea of why not 1 email gets sent every hour? //mega wp noob

you're checking if event scheduled , when it's not found you're registering under different name. mismatch what's causing run on every page load.

change this:

if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) { 

to this:

if ( ! wp_next_scheduled( 'prefixhourlyevent' ) ) { 

i'd @ whether can check schedule once rather on each page load. if writing plugin example fire on plugin activation hook.

further reading: https://codex.wordpress.org/function_reference/wp_schedule_event


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -