Make WordPress Core

Opened 7 weeks ago

Closed 7 weeks ago

#63479 closed defect (bug) (fixed)

current_filter() can return false if fatal

Reported by: kkmuffme's profile kkmuffme Owned by: jorbin's profile jorbin
Milestone: 6.9 Priority: normal
Severity: trivial Version:
Component: Plugins Keywords: has-patch commit has-test-info
Focuses: docs Cc:

Description

current_filter() (and current_action()) have a @return string
However both can return false when used in an exception handler callback, if there's a fatal/uncaught exception and the current code is not run on an action/filter - e.g. in WP core itself or if a plugin manually loads WP and executes code after requiring wp-load.php

The @return should be changed to string|false

Change History (7)

This ticket was mentioned in PR #8840 on WordPress/wordpress-develop by @debarghyabanerjee.


7 weeks ago
#1

  • Keywords has-patch added

Trac Ticket: Core-63479

## Problem
The @return annotations for current_action() and current_filter() are currently defined as string, but both functions can return false when called outside the context of any action or filter. This can occur in specific scenarios such as:

  • Within an exception handler after a fatal or uncaught exception.
  • When WordPress is manually loaded (e.g., via wp-load.php) and code is executed before any hooks have been triggered.

This mismatch between the documented return type and actual behavior can lead to incorrect assumptions and potential runtime issues.

## Solution

Update the @return annotations for both current_action() and current_filter() to string|false.

#2 @mukesh27
7 weeks ago

  • Keywords changes-requested added

#3 @SirLouen
7 weeks ago

  • Keywords needs-test-info added

@debarghyabanerjee can you provide a testing case that reproduces the issue your patch is informing? (false for each function). You can add some code

Last edited 7 weeks ago by SirLouen (previous) (diff)

#4 @debarghyabanerjee
7 weeks ago

Hi @SirLouen, you can try out this snippet by simply adding it in the functions.php, please let me know in case of any doubts. Thanks.

<?php
set_exception_handler(
    function ( $exception ) {
        var_dump( current_filter() );
        var_dump( current_action() );
        wp_die();
    }
);

throw new Exception( 'Test exception' );

#5 follow-up: @peterwilsoncc
7 weeks ago

  • Focuses docs added
  • Keywords commit added; changes-requested removed
  • Milestone changed from Awaiting Review to 6.9

The linked pull request looks good to me.

@SirLouen You can see the functions returning false via wp-cli wp eval "var_dump( current_filter() );" and wp eval "var_dump( current_action() );".

#6 in reply to: ↑ 5 @SirLouen
7 weeks ago

  • Keywords has-test-info added; needs-test-info removed

Replying to peterwilsoncc:

The linked pull request looks good to me.

@SirLouen You can see the functions returning false via wp-cli wp eval "var_dump( current_filter() );" and wp eval "var_dump( current_action() );".

Basically, the idea to trigger false is calling current_filter in the middle of nowhere
✅ Reproduced.

#7 @jorbin
7 weeks ago

  • Owner set to jorbin
  • Resolution set to fixed
  • Status changed from new to closed

In 60258:

Plugins: Clarify documentation for when current_action and current_filter can return false.

If current_action or current_filter is called outside of an action/filter, then these functions will return false.

Props debarghyabanerjee, mukesh27, peterwilsoncc, SirLouen.
Fixes #63479.

Note: See TracTickets for help on using tickets.