Opened 4 years ago
Closed 4 years 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)
Change History (9)
#2
@
4 years 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
@
4 years 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()
#4
@
4 years ago
- Focuses coding-standards removed
- Milestone set to 5.6
- Resolution worksforme deleted
- Status changed from closed to reopened
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.