WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#14728 closed defect (bug) (invalid)

capabilities.php does not include pluggable.php

Reported by: BlaenkDenum Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.1
Component: General Keywords: reporter-feedback close
Focuses: Cc:

Description

I have a plugin that calls is_super_admin as defined in capabilities.php. In multi-site installations, when I enable the plugin, and call is_super_admin to see if the user is a network admin, I get "Call to undefined function wp_get_current_user" in capabilities.php on line 1208. After searching around and trying to fix my plugin, I found a solution which is to add "require_once('pluggable.php')" to the top of capabilities.php.

Is this a bug with WordPress? If so, can it be fixed? This fatal error requires people to have to rename/delete the plugin directory which isn't all that user-friendly.

Change History (4)

comment:1 follow-up: @nacin5 years ago

  • Keywords reporter-feedback close added
  • Severity changed from critical to normal

You're calling is_super_admin() too early. The function may exist, but you can't reliably perform any user actions until the initialization hook.

comment:2 in reply to: ↑ 1 @BlaenkDenum5 years ago

Replying to nacin:

You're calling is_super_admin() too early. The function may exist, but you can't reliably perform any user actions until the initialization hook.

Is the call to is_super_admin the problem though? From what I can tell, the function is being correctly called, it's when it gets to within the function that it calls wp_get_current_user that it crashes.

Is there any way I can resolve this? I tried passing in the user id myself so that it didn't take that conditional path. I did something like this:

global $current_user;
$current_user = wp_get_current_user();
if (function_exists('is_super_admin')) {
    return is_super_admin($current_user->ID);
}

But even then, now my plugin can't find the function (I would have to include pluggable.php myself).

Is there any solution to this problem?

comment:3 @nacin5 years ago

It's giving you a fatal error because it is too early to perform tasks or actions based on the user that is logged in. The alternative is to silently fail, but it's much better to allow functions to throw fatal errors that way plugin authors realize it doesn't work.

Please don't include pluggable.php yourself -- that can break a lot of plugins. The proper way to do this is to make sure your plugin doesn't execute anything when it is included, beyond add_action/add_filter calls. Then start processing your plugin on the initialization hook (init).

A plugin should be encapsulated so as not to run any heavy code when it is first included, and instead wait for hooks. This is but one reason why.

comment:4 @scribu5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.