Why does do_action pass a blank string as the first parameter if no $arg is set?

When we fire up:

do_action( 'foo' );

we are actually calling:

do_action( 'foo', '' );

That’s because how it’s defined; With the empty string as the default input argument:

function do_action($tag, $arg = '') {
    // ...
}

So the action’s callback get’s the empty string as it’s first input argument.

This has been like this since at least WordPress version 1.5.

Since WordPress 4.6, do_action() is a wrapper of WP_Hook::do_action();

What if …

It looks like if it would be defined without the argument part as:

function do_action($tag) {
    // ...
}

and with corresponding modifications to this part:

$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
    $args[] =& $arg[0];
else
    $args[] = $arg;

then we might use the default value, of the first input argument, in our callback.

Leave a Comment