0

I want to send an email every second using my custom plugin for testing purpose. I have written the code to schedule this event but i m not getting emails. I am not sure if I have scheduled it correctly, but apparently everything seems fine. here is my code, I have written this code in plugin.

<?php


register_activation_hook(__FILE__, 'my_activation');

function isa_add_cron_recurrence_interval( $schedules ) {

    $schedules['every_second'] = array(
    'interval'  => 1,
    'display'   => __( 'Every second', 'textdomain' )
    );

    return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_cron_recurrence_interval' );

function my_activation() {
    if (! wp_next_scheduled ( 'my_event' )) {
    wp_schedule_event(time(), 'everysecond', 'my_event');
    }
}

add_action('my_event', 'do_this');

function do_this() {
    mail('haris.4090@gmail.com','Testing','testing email from plugin');
}
  • Do you have a cron that runs every second / is the website visited every second? – kero Jul 13 '17 at 12:39
  • @kero No not every second, does it work only when website is visited? – Syed Haris Ali Ghaznavi Jul 13 '17 at 12:47
  • 1
    If you're using wp cron (which is the default), then yes. Read articles like this to understand why. You can disable wp cron like so, but even then, most hosting providers don't allow for cronjobs/crontabs every second (usually every x minutes) – kero Jul 13 '17 at 12:49
  • 1
    Thought about asking Isabel from which you taken partly the code?! – Charles Jul 13 '17 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.