WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 8 months ago

#14881 new defect (bug)

do_action should not pass empty string by default

Reported by: nacin Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch 3.2-early
Focuses: Cc:

Description (last modified by nacin)

function test( $a = true ) {
   var_dump( $a );
}
add_action( 'test', 'test' );
add_action( 'test', 'test', 10, 0 );
do_action( 'test' );

Second example is bool(true) as expected, but the first is a zero-length string.

In this case, since do_action() is not passing any arguments, then no arguments should be passed to test() -- even if by default, one argument gets passed.

The issue here is how do_action() is defined: function do_action( $tag, $arg = '' );. There's our zero-length string. Of note, apply_filters() does not have this same issue.

The solution looks something like what I have attached. Has yet to be benchmarked.

Of note, this does not appear to have any direct implications for passing all args by default, as proposed in #14671, but I wanted to reference it anyway.

Attachments (1)

14881.diff (1.0 KB) - added by nacin 6 years ago.

Download all attachments as: .zip

Change History (9)

@nacin
6 years ago

#1 @nacin
6 years ago

  • Description modified (diff)

#2 @hakre
6 years ago

Please take not of this ticket as well: #14789

#3 @hakre
6 years ago

Related: #14671 - Deprecate the "accepted args" argument in add_filter() and add_action()

#4 @nacin
6 years ago

  • Milestone changed from Awaiting Review to 3.1

#5 @nacin
6 years ago

  • Keywords 3.2-early added
  • Milestone changed from 3.1 to Future Release

#7 @mnelson4
16 months ago

Just encountered this gotcha last week.. What's the holdup? Are there plugins that depend on this bug?

#8 @kjbenk
8 months ago

Based off of your diff it seems that you are returning an empty array instead of an empty string. Would you want to return NULL instead? An empty array might confuse developers into thinking that it is actually an argument to the action. However, a NULL value is much more related to an error. Please let me know your thoughts @nacin.

Last edited 8 months ago by kjbenk (previous) (diff)
Note: See TracTickets for help on using tickets.