Make WordPress Core

#63479 closed defect (bug) (fixed)

current_filter() can return false if fatal

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

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.


18 months 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
18 months ago

  • Keywords changes-requested added

#3 @SirLouen
18 months 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 or use 3v4l.org

Version 0, edited 18 months ago by SirLouen (next)

#4 @debarghyabanerjee
18 months 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
18 months ago

  • Focuses docs added
  • Keywords commit added; changes-requested removed
  • Milestone Awaiting Review → 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
18 months 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
18 months ago

  • Owner set to jorbin
  • Resolution → fixed
  • Status new → 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.