如何使用 WordPress SHORTINIT 获取当前登录用户 [英] How Can I Get the Current Logged-In User with WordPress SHORTINIT

查看:33
本文介绍了如何使用 WordPress SHORTINIT 获取当前登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个由 AJAX 函数调用的 PHP 脚本.我不想使用内置的 WordPress AJAX API,因为它会花费太长时间,因为我要上传很多图像.我正在使用 SHORTINIT 执行部分 WordPress 加载,如下所示:https://wordpress.stackexchange.com/questions/173002/how-declare-ajax-functions-ussing-shortinit

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

我需要检查当前登录用户的用户名.有人可以列出我需要在我的自定义 PHP 文件中包含/要求的文件以及它们的顺序吗?谢谢!

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!

推荐答案

因为我无法评论(新用户).我用于使 is_user_logged_in()current_user_can() 工作的最终解决方案(单个 WP 安装)如下所示.我们require('wp-load.php') first (跳过load-blog-header.php中的wp()),并且获取 ABSPATH 常量,然后手动包含完全所有需要的东西.

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.

使用 define('SHORTINIT', true) + require('wp-load.php') + 手动包含:

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

页面加载:1.05 sek - 包含的文件:43 个文件

Pageload: 1.05 sek - included files: 43 files

比较:使用 ONLY require('wp-load.php'):

页面加载:1.35 sek - 包含的文件:419 个文件

Pageload: 1.35 sek - included files: 419 files

时间差 (0.3 sek) 可能因安装和 PHP 引擎而异,但在一个页面加载上验证许多请求时 - 事情加起来了!

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

记住使用对 WP 安装目录的相对调用.从 Wordpress 自定义插件目录,在一个子目录级别内,正常安装,路径应该是:

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';

那么:

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' );

此后,即可访问用户验证.对于其他任务,r取消一两个请求,跟踪其他需要的文件不值 0.3 sek.跳过 SHORTINIT 常量和手动混乱.

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.

这篇关于如何使用 WordPress SHORTINIT 获取当前登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆