WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#46586 new feature request

`determine_current_user` filter only run once per request

Reported by: Tyrannous Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Users Keywords:
Focuses: Cc:
PR Number:

Description

The determine_current_user filter (source:trunk/src/wp-includes/user.php#L2698) in _wp_get_current_user() (source:trunk/src/wp-includes/user.php#L2639) was introduced by @nacin in changeset:27484/trunk/src/wp-includes/pluggable.php.

It is only run once per request, i.e. if the global variable $current_user is still empty. This leads to problems when a plugin calls wp_get_current_user() early and other plugins that are executed later rely on the filter callback to be executed.

Example (pseudo code):

<?php
/**
 * Plugin Name: Plugin A
 */

add_action(
    'plugins_loaded',
    function() {
        add_filter(
            'determine_current_user',
            function($user_id) {
                // Do something here with $user_id.
                // If Plugin B is active, this will never be executed.
                return $user_id;
            }
        );
        $user = wp_get_current_user();
        // Do something here with $user.
    },
    5
);
<?php
/**
 * Plugin Name: Plugin B
 */

add_action(
    'plugins_loaded',
    function() {
        $user = wp_get_current_user();
        // Do something here with $user.
    },
    1
);

My suggestion is to either move the filter up in the function so that it is executed every time the function is called, call the filter before any return statements, or introduce a new filter that is executed at the beginning of the function (e.g. pre_determine_current_user).

What do you think of that? Thanks!

Attachments (2)

plugin-a.php (398 bytes) - added by Tyrannous 9 months ago.
Plugin A. It's determine_current_user filter will never be executed if Plugin B is active.
plugin-b.php (197 bytes) - added by Tyrannous 9 months ago.
Plugin B. If it's active, Plugin A's determine_current_user filter will never be executed.

Download all attachments as: .zip

Change History (3)

@Tyrannous
9 months ago

Plugin A. It's determine_current_user filter will never be executed if Plugin B is active.

@Tyrannous
9 months ago

Plugin B. If it's active, Plugin A's determine_current_user filter will never be executed.

Note: See TracTickets for help on using tickets.