WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#21612 closed defect (bug) (invalid)

wp-settings loads plugins before pluggable.php is included

Reported by: kingjeffrey Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: General Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

When manually initializing WordPress (like in a page that handles AJAX requests) to access all WordPress functions, it is typical to begin the file with:

	define('WP_USE_THEMES', FALSE);
	require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');

However, if a plugin calls the get_userdata() function, the page will deliver this fatal error:

	Fatal error: Call to undefined function get_userdata() in [...]/wp-includes/user.php on line 1402

The "Maintenance" plugin is an example of one that causes this issue.

When the plugin is loaded in wp-settings.php line 198, it calls the get_userdata() function. But the get_userdata() function is not defined until wp-includes/pluggable.php is included on line 202 of wp-settings.

So this issue is not limited to calling get_userdata(), but any function defined in wp-includes/pluggable.php.

A simple fix to this, assuming wp-includes/pluggable.php does not depend on the plugins being loaded (which it very may well), is to swap the order of loading in wp-settings.php

Change History (2)

#1 @SergeyBiryukov
5 years ago

  • Description modified (diff)

#2 @nacin
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  1. Pluggable functions are meant to be "pluggable," meaning, a plugin can define them before core does. We cannot change the order without breaking plugins, possibly causing fatal errors, and killing the concept of pluggable functions all together (as much as that last bit is appetizing).
  1. Don't do Ajax requests through a separate page like that. Use admin-ajax.php or a query variable. http://codex.wordpress.org/AJAX_in_Plugins. Any time you include wp-config, wp-load, or wp-blog-header, you're probably doing it wrong.
  1. Don't do anything in the main body of the plugin, beyond add_action(). Wait until the plugins_loaded hook, or init. Then you wouldn't have this problem.
Note: See TracTickets for help on using tickets.