Opened 6 years ago
Closed 6 years ago
#51605 closed defect (bug) (fixed)
Cron hook does not receive $args array
| Reported by: | chadreitsma | Owned by: | johnbillion |
|---|---|---|---|
| Priority: | normal | Milestone: | 5.6 |
| Component: | Cron API | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: | docs |
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
@
6 years ago
- Milestone Awaiting Review
- Resolution invalid → 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
@
6 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
@
6 years ago
- Focuses coding-standards removed
- Milestone → 5.6
- Resolution worksforme
- Status closed → reopened
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.