I am having trouble wrapping my head around these two functions. I understand do_action()
but I don’t see clearly when do_action_ref_array()
would be useful. Couldn’t we pass an array to do_action()
as well?
Leave a Reply
You must be logged in to post a comment.
If you pass an array to
do_action_ref_array()
, each elementâs value of that array will be passed as a separate parameter to the callback. The keys are lost.If you pass an array to
do_action()
, the complete array will be passed as one single parameter to the callback. The keys stay intact.Letâs look at the differences between
do_action()
anddo_action_ref_array()
in a simple plugin:Result:
do_action_ref_array()
was introduced 2006 to handle parameters passed by reference better in PHP 4. A review of the function 2011 was closed as wontfix. But the conclusion was not to use for new functions in WordPress anymore, because in PHP 5 you can do that withdo_action()
too. The problem from 2006 is not a problem nowadays.Do not use
do_action_ref_array()
in your custom hooks, but learn how to read it.