How Can I Get the Current Logged-In User with Word

2019-09-04 05:56发布

问题:

I'm running a PHP script called by an AJAX function. I do not want to use the built-in WordPress AJAX API because it would take way too long, since I'm uploading a lot of images. I'm using SHORTINIT to do a partial WordPress load like this: https://wordpress.stackexchange.com/questions/173002/how-declare-ajax-functions-ussing-shortinit

I need to check the username of the current logged-in user. Can someone list the files I need to include/require in my custom PHP file and the order they should be in? Thanks!

回答1:

As I cant comment (new user). The final soultion (single WP install) I use for making is_user_logged_in() and current_user_can() work, is as follow below. We require('wp-load.php') first (to skip wp() in load-blog-header.php), and get ABSPATH constant then, manually includes exactly all the stuff needed.

Using define('SHORTINIT', true) + require('wp-load.php') + manually includes:

Pageload: 1.05 sek - included files: 43 files

Comparing: Using ONLY require('wp-load.php'):

Pageload: 1.35 sek - included files: 419 files

The time difference (0.3 sek) might differ from installs and PHP engines, but while validating many requests on one pageload -things adds up!

Remember to use relative call to WP installed dir. From a Wordpress custom plugin dir, inside one subdir level, normal install, a path should be like:

$wordpress = '../../../../wp-load.php';

Then:

define('SHORTINIT', true);
include_once $wordpress;

require_once ( ABSPATH . WPINC . '/class-wp-user.php' );
require_once ( ABSPATH . WPINC . '/class-wp-roles.php' );
require_once ( ABSPATH . WPINC . '/class-wp-role.php' );
require_once ( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
require_once ( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );
require_once ( ABSPATH . WPINC . '/formatting.php' );
require_once ( ABSPATH . WPINC . '/capabilities.php' );
//require_once ( ABSPATH . WPINC . '/query.php' ); // - might be useful
require_once ( ABSPATH . WPINC . '/user.php' );
require_once ( ABSPATH . WPINC . '/meta.php' );

wp_cookie_constants();

require_once ( ABSPATH . WPINC . '/vars.php' );
require_once ( ABSPATH . WPINC . '/kses.php' );
require_once ( ABSPATH . WPINC . '/rest-api.php' );
require_once ( ABSPATH . WPINC . '/pluggable.php' );

After this, user validation is accessable. For other task, running on one or two requests, tracking down other needed files is not worth 0.3 sek. Skip the SHORTINIT constant and manually clutter.



回答2:

Maybe a bit late but I will post it here so it might be useful to other people.

I needed a quick light weight way to load the minimum of Wordpress to figure out the user in a separate API next to the Wordpress install. For me this code works with Wordpress 4.8

NOTE: it is only used in this way. It can be that if you need to do more Wordpress things, you are missing some includes.

// load minimum wordpress to load the user
define('SHORTINIT', true);

/** Define ABSPATH as this files directory */
define( 'ABSPATH', dirname(__FILE__) . '/' );

//WP config file
require ('wp-config.php');

// Run the installer if WordPress is not installed.
wp_not_installed();

require( ABSPATH . WPINC . '/class-wp-user.php' );
require( ABSPATH . WPINC . '/class-wp-roles.php' );
require( ABSPATH . WPINC . '/class-wp-role.php' );
require( ABSPATH . WPINC . '/class-wp-session-tokens.php' );
require( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' );

require( ABSPATH . WPINC . '/formatting.php' );
require( ABSPATH . WPINC . '/capabilities.php' );
require( ABSPATH . WPINC . '/query.php' );
require( ABSPATH . WPINC . '/user.php' );
require( ABSPATH . WPINC . '/meta.php' );

// Define constants after multisite is loaded. Cookie-related constants may be overridden in ms_network_cookies().
wp_cookie_constants( );

// Create common globals.
require( ABSPATH . WPINC . '/vars.php' );
require( ABSPATH . WPINC . '/kses.php' );
require( ABSPATH . WPINC . '/rest-api.php' );
require( ABSPATH . WPINC . '/pluggable.php' );
require('wp-load.php');

$user = wp_get_current_user();
var_dump($user);

I used the code here as a starting point but that didn't work under the current Wordpress version for me: https://wordpress.stackexchange.com/questions/28342/is-there-a-way-to-use-the-wordpress-users-but-without-loading-the-entire-wordpre