Make WordPress Core

Opened 6 years ago

Closed 7 months ago

Last modified 7 months ago

#49678 closed defect (bug) (wontfix)

do_action default first parameter

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

Description

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?

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

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

Change History (5)

#1 @SergeyBiryukov
6 years ago

  • Component changed from General to Plugins

#2 @donmhico
6 years ago

  • Keywords dev-feedback added

Hi @Tkama,

Navigating the codebase, seems like this line is responsible - https://core.trac.wordpress.org/browser/trunk/src/wp-includes/plugin.php#L472

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

#3 @Tkama
6 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:

<?php
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.

#4 @callumbw95
7 months ago

  • Resolution set to wontfix
  • Status changed from new to closed

Hi @Tkama,

I have taken a quick look at this and the do_action() function has been a fundamental part of the WordPress plugin API for many major releases, and as of such countless plugins and themes rely on its existing behaviour.

This ticket proposes changing the default value passed to an action's callback. Currently, when do_action( 'hook_name' ) is called without any arguments, the callback function receives an empty string "" as its first parameter, and I believe the intent here is to pass null when nothing is passed.

While this change would create more consistency with the apply_filters API, it would introduce a significant backward-compatibility break.

Developers have relied on the default empty string behavior for years, using checks like if ( empty( $param ) ) or if ( '' === $param ) to determine if a value was explicitly passed to do_action. Changing this fundamental behavior would alter the logic in a vast number of plugins and themes, leading to unpredictable results and bugs.

For these reasons, I'm closing this ticket as wontfix, but of course if you have any further feedback please feel free to reopen the ticket.

#5 @johnbillion
7 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.