WordPress.org

Make WordPress Core

Opened 6 weeks ago

Closed 5 weeks ago

#51605 closed defect (bug) (fixed)

Cron hook does not receive $args array

Reported by: chadreitsma Owned by: johnbillion
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: Cron API Keywords: has-patch
Focuses: docs Cc:

Description

I have the following code:

add_action('my_cron_hook', 'do_cron_hook');
function do_cron_hook($args) {

  print_r($args);
  exit();

}

I have scheduled the hook to fire at a specific time with: wp_schedule_single_event() with the $args array('message_id' => 1234, 'test' => 'test');

When the hook fires - print_r only displays "1234" like the array has been disassociated. Why is the array not passed to the hook?

Attachments (1)

51605.diff (4.3 KB) - added by peterwilsoncc 5 weeks ago.

Download all attachments as: .zip

Change History (9)

#1 @chadreitsma
6 weeks ago

  • Resolution set to invalid
  • Status changed from new to closed

Sorry, I see it will pass them as individual arguments - that was confusing to me, why not just pass the array? Please mark as resolved, or remove.

Cheers,
C.

#2 @johnbillion
6 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution changed from invalid to worksforme

That's correct, each parameter in $args gets passed to the event callback as an individual parameter. You should use a numerically indexed array instead of an associative array in your code.

The docs for this do explain this but it's quite difficult to explain succinctly, and you're definitely not the first person to have this problem.

While the behaviour cannot be changed, the docs can certainly be improved, so if you have any suggestions they're very welcome.

#3 @chadreitsma
6 weeks ago

Thanks John, that makes sense. You're right, it is hard to explain succinctly, haha.

I have attempted to make it a little more understandable. For your consideration...

Current description:
(array) (Optional) Array containing each separate argument to pass to the hook's callback function. Default value: array()

New description:
(array) (Optional) Array containing arguments to pass to the hook's callback function. Each value in the array is passed to the callback as an individual parameter. The array keys are ignored. Default value: array()

Last edited 6 weeks ago by chadreitsma (previous) (diff)

#4 @SergeyBiryukov
6 weeks ago

  • Focuses coding-standards removed
  • Milestone set to 5.6
  • Resolution worksforme deleted
  • Status changed from closed to reopened

@peterwilsoncc
5 weeks ago

#5 @peterwilsoncc
5 weeks ago

51605.diff

  • Improved description for $args array in scheduling and rescheduling functions props @chadreitsma
  • Extended description for $args array in wp_clear_scheduled_hook() to match those used in other functions that use the $args array as part of a getter.

#6 @peterwilsoncc
5 weeks ago

  • Keywords has-patch added

#7 @johnbillion
5 weeks ago

  • Version 5.5.1 deleted

#8 @johnbillion
5 weeks ago

  • Owner set to johnbillion
  • Resolution set to fixed
  • Status changed from reopened to closed

In 49357:

Cron API: Improve the inline documentation for the args passed to the cron event scheduling functions.

Props chadreitsma, peterwilsoncc

Fixes #51605

Note: See TracTickets for help on using tickets.