我有一个自定义PHP文件,我想在其上使用一些WordPress函数,如wp\\u get\\u current\\u user()。我试过要求wp加载。php,但这会显著增加加载时间,因为它加载了所有WP函数。
无论如何,我是否只能使用WP提供的功能?
谢谢,埃米尔
我有一个自定义PHP文件,我想在其上使用一些WordPress函数,如wp\\u get\\u current\\u user()。我试过要求wp加载。php,但这会显著增加加载时间,因为它加载了所有WP函数。
无论如何,我是否只能使用WP提供的功能?
谢谢,埃米尔
您可以让Wordpress将数据推送到会话或自定义PHP,而不是每次需要向用户查询某些数据时都尝试加载Wordpress。我不确定您还想调用什么其他函数。
另一种方法是使用Wordpress REST API, 看看authentication section.
wp_get_current_user
例如:function wp_get_current_user() {
return _wp_get_current_user();
}
这导致我们_wp_get_current_user
:function _wp_get_current_user() {
global $current_user;
if ( ! empty( $current_user ) ) {
if ( $current_user instanceof WP_User ) {
return $current_user;
}
// Upgrade stdClass to WP_User
if ( is_object( $current_user ) && isset( $current_user->ID ) ) {
$cur_id = $current_user->ID;
$current_user = null;
wp_set_current_user( $cur_id );
return $current_user;
}
// $current_user has a junk value. Force to WP_User with ID 0.
$current_user = null;
wp_set_current_user( 0 );
return $current_user;
}
if ( defined(\'XMLRPC_REQUEST\') && XMLRPC_REQUEST ) {
wp_set_current_user( 0 );
return $current_user;
}
/**
* Filters the current user.
*
* The default filters use this to determine the current user from the
* request\'s cookies, if available.
*
* Returning a value of false will effectively short-circuit setting
* the current user.
*
* @since 3.9.0
*
* @param int|bool $user_id User ID if one has been determined, false otherwise.
*/
$user_id = apply_filters( \'determine_current_user\', false );
if ( ! $user_id ) {
wp_set_current_user( 0 );
return $current_user;
}
wp_set_current_user( $user_id );
return $current_user;
}
我们可以看到WordPress在获取当前用户方面做了很多“幕后”工作,因此将此函数作为一个独立的方法实现可能不会让您走得太远。您还应该看看SHORTINIT
常量-如果在wp-config.php
, 它加载WordPress的某些部分,然后退出。我不确定它是否能满足您的需要,但它可能值得一看。
我在函数中不断遇到此代码的未定义索引错误。我做错了什么?function hire_more(){ $hire = $_GET[\'hire_more\']; write_log($hire); } add_action(\'wp_loaded\',\'hire_more\');