Making Plugins bbPress and BuddyPress Compatible

Lately, I’ve been coming across quite a few plugins that cause this notice to display.

Notice: bbp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init(). Please see Debugging in WordPress for more information. (This message was added in version 2.3.) in /demosite/wp-includes/functions.php on line 3547 Notice: bp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init(). Please see Debugging in WordPress for more information. (This message was added in version 1.7.) in /demosite/wp-includes/functions.php on line 3547

While developing, this notice will display on every page both front-end and back. The reason why this notice displays is due to code trying to access user information before the WordPress action init is called.

For example, if you want to get the current user’s display name and you have the code:

$current_user = wp_get_current_user();
$display_name = $current_user->display_name;

This code will cause the notices to show. Fortunately, the fix is relatively simple. A good habit to get into is to wrap all of your code in a function.


function display_access_message() {
$current_user = wp_get_current_user();
$display_name = $current_user->display_name;
}

add_action( 'init', 'display_access_message' );

Essentially, there shouldn’t be any reason for anything to need to run before init other than to queue included files or actions and filters.

1 reply

Trackbacks & Pingbacks

  1. […] coding practices that demonstrates a lack of understanding of the WordPress workflow. One of the biggest culprits is not wrapping code into the appropriate actions or […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply