[wp-trac] [WordPress Trac] #46586: `determine_current_user` filter only run once per request

WordPress Trac noreply at wordpress.org
Thu Mar 21 09:23:54 UTC 2019


#46586: `determine_current_user` filter only run once per request
-----------------------------+-----------------------------
 Reporter:  Tyrannous        |      Owner:  (none)
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Users            |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 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
 <?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
 <?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!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46586>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list