Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#49678 new defect (bug)

do_action default first parameter

Reported by: tkama's profile Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.2
Component: Plugins Keywords: dev-feedback
Focuses: Cc:


This code looks like bug: we don't pass any parameters to action but the first parameter is passed anyway and have the value (empty string). Why?

add_action( 'action_name', function( $first ){
        var_dump( $first );
} );

do_action( 'action_name' ); //> string(0) ""

Change History (3)

#1 @SergeyBiryukov
4 years ago

  • Component changed from General to Plugins

#2 @donmhico
4 years ago

  • Keywords dev-feedback added

Hi @Tkama,

Navigating the codebase, seems like this line is responsible -

While it's just easy to remove the if clause, i'm not sure if it won't introduce any compatibility issues.

#3 @Tkama
4 years ago

@donmhico thanks. There is no difficulties to find the reason and fix it for me. The idea of this ticket: maybe it's better to fix core code for more predictable behavior of such code.

For example:

add_action( 'action_name', function( $first = 'foo' ){
     // some logic

     echo $first
} );

do_action( 'action_name' );

As we can see. We don't pass any params to

do_action( 'action_name' );

and we expect the code print foo but not ''.

Such hook-call logic we can use, for example, for plugins when we want wrap some plugin function, for save plugin deactivation when plugin function is used inside the theme template files.

Note: See TracTickets for help on using tickets.