Skip to content
Advertisement

Scheduling event in wordpress starting from very next monday

I have made a plugin to send weekly newsletters. The goal is that the plugin will shoot email on every Monday.

I have few confusions here:

1 – Please have look at below code

function my_cron_definer($schedules){  
    $schedules['weekly'] = array('interval'=> 604800,
        'display'=>  __('Once Every week')  
        );  
    return $schedules;
}

add_filter('cron_schedules','my_cron_definer');    

wp_schedule_event($timestamp, 'weekly', 'informantweeklynewsletter');

add_action('informantweeklynewsletter','informant_main_action');

So, I made my custom recurrence ‘weekly’. However I am not sure what should I provide to the $timestamp variable so, that it will start working from Monday regardless of when the plugin is activated. In my plugin I have not defined any functions for activation and deactivation as I don’t think I need them.

My second question is: The wordpress cron is different from normal cronjobs as it requires visit on website. Looking at the above code I need to know what are the possibilities that my plugin will not work. And what solution can I make? to work my informant_main_action method every Monday.

Thank You

Advertisement

Answer

assuming all else is correctly set up…you can use

strtotime('nextmonday');

as your timestamp. You do need to put this in a activation hook so it only runs once, you can check if the hook exists

if( ! wp_next_scheduled( 'informantweeklynewsletter' ) )
    wp_schedule_event($timestamp, 'weekly', 'informantweeklynewsletter');

Otherwise, you will a new cron job set up each time someone visits your page…..

You can supplement this with a real cron job to visit your page on a weekly basis. if you have cpanel you can use something like this (note your path will have to be updated on this). The * and numbers are when you want it to run so its min, hour, day, month, weekday

* 2 * * 7 wget -O - http://yoursite.com/tasks.php >/dev/null 2>&
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement