0

I am trying to get a simple php function working with cron (based on a tutorial). I have the following:

<?php if (!wp_next_scheduled('please_just_work')) {
 wp_schedule_event( time(), 'daily', 'please_just_work' );
}
add_action ( 'please_just_work', 'my_task_function' );
function my_task_function() { 
$test_score = 30;
update_field('score', $test_score );
} ?>

I can see the cron job in the Crontrol plugin, and I know that the site has to have a hit for the cron job to work, but it's just not firing. If I press "run now" from Crontrol and reload a page, it still doesn't fire. I really have no idea why:/

If anyone can help or point me in the right direction, I would really appreciate it!

1
  • Please don't abuse the php tag. I'm busy retagging all your posts. Please read the tag descriptions before using them. Thank you :-) – Pieter Goosen Feb 5 '15 at 13:02
1

Looking at your function:

function my_task_function() { 
    $test_score
    update_field('score', $test_score );
}

$test_score is not defined here, it should be $test_score = 'something'; Left the way you have it now will generate php warnings.

Also the update_field function used in this context (in a cron) will require you to pass a third argument specifying which post/page contains the field that you want to update. See the full docs on that function here: Advanced Custom Fields: Update Field.

1
  • Oops, my bad! Updated accordingly:) Thanks for the link, will have a read through now – Guit4eva Nov 8 '14 at 14:39
0

The print() output string into current page view. But in the case of the WP cron you never see that page view. It is executed asynchronously and is never sent to your browser.

You should be monitoring if it runs in some other way, for example writing a log to a file or saving something like "Last time run was..." to an option.

1
  • I updated my original question. Should that run correctly with cron? ie, update the ACF score field? (it updates correctly without cron) – Guit4eva Nov 8 '14 at 14:17

Your Answer

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

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