username_exists wordpress 中的函数抛出 500 错误(函数未定义),尽管 require/include pluggable.php 和 user.php
username_exists function in wordpress throwing 500 error (function not defined), despite require/include pluggable.php and user.php
在我的自定义插件 php 中,尝试调用核心 wordpress 函数 username_exists()
会抛出 500 错误,我认为这是由于未定义该函数引起的。
我的代码中失败的行是:
$unres = username_exists($unt);
我已经确认这不是由空参数引起的问题,因为以下按预期工作:$unres = $unt;
我该如何解决这个问题?
我尝试了以下所有答案和评论中的所有解决方案:
Function username_exists() can't be accessed without logging in to wordpress
WordPress plugin admin page - using WordPress function in linked php file
How do I call the basic WordPress header files?
wordpress plugin -> Call to undefined function wp_get_current_user()
我已成功将以下 php 添加到 include/require 这些文件中(但这没有帮助):
require realpath("../../../../../wp-includes/pluggable.php");
require realpath("../../../../../wp-includes/user.php");
require realpath("../../../../../wp-admin/includes/user.php");
如果我包含或要求以下内容,则会产生致命的站点错误:
require realpath("../../../../../wp-load.php");
我的 php 中是否有一些其他核心文件需要参考 'include',除了上面列出的那些(例如,由于写了这些问题后的新 WP 版本)? (我使用的是 WP v5.5)。
我是否需要将通话背景化? (例如引用命名空间、调用 public/global 等?)
(注意:该站点还使用 Ultimate Member 插件,不确定这是否会以任何方式覆盖核心功能名称?)
谢谢。
而不是 运行宁原始 PHP 代码,在 WordPress 的上下文中 运行 您的代码确实是一个最佳实践。 WordPress 有 many APIs available and for a JavaScript-based call the REST 可能是最好的选择。
下面是非常简单的代码,用于注册 REST 路由并针对核心 username_exists
函数测试提供的参数。我包含了内联注释,应该可以解释所有内容,但是一旦您删除它们并折叠一些空格,您就会看到它只有 20 行左右的代码。
// Always register routes in this action
add_action(
'rest_api_init',
// My IDE is happiest when I make my functions static, but that is not a requirement
static function () {
register_rest_route(
// The shared namespace for all route in this plugin
'ed2/v1',
// The path with optional parameters for this specific route
'/username/(?P<username>.*?)',
[
// We only accept GET, but could be POST or an array of methods
'methods' => 'GET',
// The function to run
'callback' => static function (WP_REST_Request $request) {
// Our parameter was named above so we can fetch it here
$username = $request->get_param('username');
// Return an array, the API will handle turning it into a JSON response
return [
'username' => $username,
'exists' => username_exists($username),
];
},
]
);
}
);
在您的浏览器中,您现在应该可以转到 http://example.com/wp-json/ed2/v1/username/test
,它应该 return(假设您没有 test
的用户名):
{"username":"test","exists":false}
如果您更改为确实存在的用户名,例如 https://example.com/wp-json/ed2/v1/username/user@example.com
您应该得到:
{"username":"user@example.com","exists":1}
为了重申我在开头所说的话,我区分了调用 WordPress 函数的 PHP 代码通常需要“唤醒 WordPress”,以及调用 PHP 函数的 WordPress 代码,这意味着 WordPress 是“已经醒了”。您 可以 运行 PHP 通过包含 wp-load.php
启动 WordPress 的代码,但如果某些事情不起作用,您也不应该感到惊讶。哪些东西可能会损坏取决于您的环境。
在我的自定义插件 php 中,尝试调用核心 wordpress 函数 username_exists()
会抛出 500 错误,我认为这是由于未定义该函数引起的。
我的代码中失败的行是:
$unres = username_exists($unt);
我已经确认这不是由空参数引起的问题,因为以下按预期工作:$unres = $unt;
我该如何解决这个问题?
我尝试了以下所有答案和评论中的所有解决方案:
Function username_exists() can't be accessed without logging in to wordpress
WordPress plugin admin page - using WordPress function in linked php file
How do I call the basic WordPress header files?
wordpress plugin -> Call to undefined function wp_get_current_user()
我已成功将以下 php 添加到 include/require 这些文件中(但这没有帮助):
require realpath("../../../../../wp-includes/pluggable.php");
require realpath("../../../../../wp-includes/user.php");
require realpath("../../../../../wp-admin/includes/user.php");
如果我包含或要求以下内容,则会产生致命的站点错误:
require realpath("../../../../../wp-load.php");
我的 php 中是否有一些其他核心文件需要参考 'include',除了上面列出的那些(例如,由于写了这些问题后的新 WP 版本)? (我使用的是 WP v5.5)。
我是否需要将通话背景化? (例如引用命名空间、调用 public/global 等?)
(注意:该站点还使用 Ultimate Member 插件,不确定这是否会以任何方式覆盖核心功能名称?)
谢谢。
而不是 运行宁原始 PHP 代码,在 WordPress 的上下文中 运行 您的代码确实是一个最佳实践。 WordPress 有 many APIs available and for a JavaScript-based call the REST 可能是最好的选择。
下面是非常简单的代码,用于注册 REST 路由并针对核心 username_exists
函数测试提供的参数。我包含了内联注释,应该可以解释所有内容,但是一旦您删除它们并折叠一些空格,您就会看到它只有 20 行左右的代码。
// Always register routes in this action
add_action(
'rest_api_init',
// My IDE is happiest when I make my functions static, but that is not a requirement
static function () {
register_rest_route(
// The shared namespace for all route in this plugin
'ed2/v1',
// The path with optional parameters for this specific route
'/username/(?P<username>.*?)',
[
// We only accept GET, but could be POST or an array of methods
'methods' => 'GET',
// The function to run
'callback' => static function (WP_REST_Request $request) {
// Our parameter was named above so we can fetch it here
$username = $request->get_param('username');
// Return an array, the API will handle turning it into a JSON response
return [
'username' => $username,
'exists' => username_exists($username),
];
},
]
);
}
);
在您的浏览器中,您现在应该可以转到 http://example.com/wp-json/ed2/v1/username/test
,它应该 return(假设您没有 test
的用户名):
{"username":"test","exists":false}
如果您更改为确实存在的用户名,例如 https://example.com/wp-json/ed2/v1/username/user@example.com
您应该得到:
{"username":"user@example.com","exists":1}
为了重申我在开头所说的话,我区分了调用 WordPress 函数的 PHP 代码通常需要“唤醒 WordPress”,以及调用 PHP 函数的 WordPress 代码,这意味着 WordPress 是“已经醒了”。您 可以 运行 PHP 通过包含 wp-load.php
启动 WordPress 的代码,但如果某些事情不起作用,您也不应该感到惊讶。哪些东西可能会损坏取决于您的环境。