Opened 21 months ago
Last modified 2 days ago
#62963 new defect (bug)
Fatal is_admin_bar_showing() - Call to undefined function is_user_logged_in()
| Reported by: | kkmuffme | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Toolbar | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
Description
When hooking a callback on e.g. "muplugins_loaded" or on "shutdown" from an mu-plugin, object cache or advanced cache and checking "is_admin_bar_showing()", you can get a fatal bc pluggable.php which is called isn't loaded yet.
Either add a function exists check for "is_user_logged_in" or load pluggable.php early?
Change History (7)
#2
@
21 months ago
shutdown occurs after the page may be output to the browser.
shutdown also happens if there's an uncaught exception for example, in which case there will never be an "init" hook if the exception happens before that.
EDIT: when WP loads a function, it needs to make sure that all things it calls have been loaded already or it will load them itself
EDIT2: or if an (mu-)plugin or theme calls exit, shutdown is called long before the init hook.
#4
@
21 months ago
- Component Administration → Toolbar
is_admin_bar_showing() runs the is_user_logged_in() condition if $show_admin_bar is not set (after checking whether it is an XML-RPC/Ajax/iframe/JSON request, an embed, or within the admin).
Could you use show_admin_bar( false ); somehow?
#5
@
21 months ago
I already worked around this issue in my code without modifying any global states, however this is something that should be addressed in WP core.
#6
@
2 days ago
I was able to reproduce this on trunk (7.2-beta4).
Steps to reproduce
- Create
wp-content/mu-plugins/wporg-62963.php:<?php add_action( 'muplugins_loaded', function () { var_dump( is_admin_bar_showing() ); } );
- Visit the front end of the site while logged out.
Result:
Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in wp-includes/admin-bar.php:1501
#4 wp-settings.php(596): do_action('muplugins_loade...')
The same happens when the function is called from a drop-in such as advanced-cache.php or object-cache.php. In admin, is_admin() returns early, so only front-end requests are affected.
Cause
is_user_logged_in() is defined in pluggable.php. That file is loaded in wp-settings.php after regular plugins and just before plugins_loaded. When $show_admin_bar has not been set yet, is_admin_bar_showing() calls is_user_logged_in() with no check.
Possible approach
Would an approach like this be acceptable, or should this be handled differently?
Return false with a _doing_it_wrong() notice when pluggable functions are not available yet. The result is not cached in the $show_admin_bar global, so later calls still work out the real value.
-
src/wp-includes/admin-bar.php
a b function is_admin_bar_showing() { 1498 1498 } 1499 1499 1500 1500 if ( ! isset( $show_admin_bar ) ) { 1501 /* 1502 * The current user cannot be determined before pluggable functions are loaded, 1503 * for example when called from a drop-in or on the 'muplugins_loaded' action. 1504 * Do not cache the result so that later calls can determine it correctly. 1505 */ 1506 if ( ! function_exists( 'is_user_logged_in' ) ) { 1507 _doing_it_wrong( 1508 __FUNCTION__, 1509 sprintf( 1510 /* translators: %s: plugins_loaded */ 1511 __( 'The admin bar visibility cannot be determined before the %s action.' ), 1512 '<code>plugins_loaded</code>' 1513 ), 1514 '7.3.0' 1515 ); 1516 1517 return false; 1518 } 1519 1501 1520 if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) { 1502 1521 $show_admin_bar = false; 1503 1522 } else {
This ticket was mentioned in PR #15852 on WordPress/wordpress-develop by @tremidkhar.
2 days ago
#7
- Keywords has-patch added
is_admin_bar_showing() calls is_user_logged_in() when the $show_admin_bar global is not set yet. is_user_logged_in() is defined in pluggable.php, which is loaded after regular plugins and just before plugins_loaded. Calling is_admin_bar_showing() earlier (from a drop-in such as advanced-cache.php or object-cache.php, or on muplugins_loaded) causes a fatal error on front-end requests:
Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in wp-includes/admin-bar.php:1501
This PR makes the function return false with a _doing_it_wrong() notice when pluggable functions are not available yet. The result is not cached in $show_admin_bar, so later calls still work out the real value.
Testing instructions
- Create
wp-content/mu-plugins/repro-62963.php:<?php add_action( 'muplugins_loaded', function () { var_dump( is_admin_bar_showing() ); } );
- Visit the front end of the site.
- Before this PR: fatal error. After this PR:
bool(false)is printed, the page loads, and withWP_DEBUGenabled a_doing_it_wrong()notice is shown.
A unit test for the early path is not included, because pluggable.php is always loaded during PHPUnit runs. The existing admin-bar test group passes.
Use of AI Tools
Claude Code was used to reproduce the issue, draft the fix, and write this description. I reviewed and tested the changes.
---
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.
🤖 Generated with Claude Code
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Calling certain functions before
initruns is not advised, and can lead to fatal errors like you've experienced.muplugins_loadedshouldn't be anything other than the very first plugin bootstrap functionality (such as adding filters), The WordPress user is not loaded at that time (So you can't check if a user setting is valid), andshutdownoccurs after the page may be output to the browser.To me, it feels like the code you're running might be the problem, and should be delayed until
initor later.Can you provide example code that shows the behaviour in a way which should be supported?