I want users of my plugin to be able to define whether a task operates daily, twice daily, hourly or not at all as per the standard WP set-up. I'm storing these as 'daily', 'twicedaily', 'hourly' or '' in the option obr_scheduled_interval.

The problem is that the task that I want 'obr_grab' runs every time there's a page visit rather than every time there's a page visit after the selected time period.

These are the relevant bits of code from the class.

function __construct(){
    register_activation_hook(__FILE__, array(&$this, 'obr_activate_scheduled_task'));
    add_action('obr_scheduled_task', array(&$this, 'obr_activate_scheduled_task'));
    add_shortcode('obr_grab', array(&$this, 'obr_grab'));
    register_deactivation_hook(__FILE__, array(&$this, 'obr_deactivate_scheduled_task'));

function obr_activate_scheduled_task() {
    // function to activate the scheduled task (if there is one);
    if (wp_next_scheduled('obr_scheduled_task')){
    $interval = get_option('obr_scheduled_interval');
    if (strlen($interval) > 0){
        wp_schedule_event(time(), $interval, 'obr_scheduled_task');

function obr_scheduled_task(){
    // if called we need to use the grab function

function obr_deactivate_scheduled_task(){
    // clear the schedule on deactivation

I've looked at core control but there are only two standard jobs there: wp_scheduled_auto_draft_delete and wp_scheduled_delete

All help much appreciated!

  • Small hint for debugging: Use the plugin "Debug Objects" and you see all crons.
    – bueltge
    Commented Feb 20, 2013 at 12:48
  • Thank you. It shows up that there are 2 standard jobs, as above.
    – user417627
    Commented Feb 20, 2013 at 14:00

1 Answer 1


It looks to me that this line

add_action('obr_scheduled_task', array(&$this, 'obr_activate_scheduled_task'));

should be

add_action('obr_scheduled_task', array(&$this, 'obr_scheduled_task'));

otherwise you are re-scheduling on every page visit.


Here is a logger that can be useful when debugging wp-cron:

function my_logger($var){
    $logfile="/tmp/logger.txt"; // EDIT this to your needs
    file_put_contents($logfile, date("Y-m-d: H:i:s",time())." --- ".print_r($var, true)."\n", FILE_APPEND);

You can use it inside your class functions with relevant debug messages,

 my_logger("My Class construction");
 my_logger("Running the cron function");

and the logfile will look like this:

2013-02-20: 14:00:43 --- My Class construction
2013-02-20: 14:00:53 --- Running the cron function

Hope this helps.

  • Thanks, I've changed that but it's still calling 'obr_grab' on every page.
    – user417627
    Commented Feb 20, 2013 at 13:55
  • did you re-activate the plugin after your changes and are you sure the function is not used elsewhere, like from the shortcode?
    – birgire
    Commented Feb 20, 2013 at 14:50
  • Hi - yes, I did reactivate it and no, the shortcode isn't being used elsewhere. I'm completely puzzled!!!
    – user417627
    Commented Feb 20, 2013 at 15:39
  • I tried your code and it is not running on every page visit on my install - I also used the logger function I added above to watch it.
    – birgire
    Commented Feb 20, 2013 at 15:44
  • Hmm, that's interesting. Many thanks for the logger function. I was running the plugin on a multisite version of WP so I've just tried it on a fresh install and it's okay!?!
    – user417627
    Commented Feb 20, 2013 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.