Php 对WP进行身份验证以获取当前用户
我试图从wordpress文件中获取用户详细信息(但使用的是同一台服务器),为此我使用了以下代码Php 对WP进行身份验证以获取当前用户,php,wordpress,authentication,Php,Wordpress,Authentication,我试图从wordpress文件中获取用户详细信息(但使用的是同一台服务器),为此我使用了以下代码 <?php define( 'WP_USE_THEMES', false ); // Do not use the theme files define( 'COOKIE_DOMAIN', false ); // Do not append verify the domain to the cookie define( 'DISABLE_WP_CRON', true )
<?php
define( 'WP_USE_THEMES', false ); // Do not use the theme files
define( 'COOKIE_DOMAIN', false ); // Do not append verify the domain to the cookie
define( 'DISABLE_WP_CRON', true ); // We don't want extra things running...
//$_SERVER['HTTP_HOST'] = ""; // For multi-site ONLY. Provide the
// URL/blog you want to auth to.
// Path (absolute or relative) to where your WP core is running
require("/var/www/yourdomain.com/htdocs/wp-load.php");
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
} else {
$creds = array();
// If you're not logged in, you should display a form or something
// Use the submited information to populate the user_login & user_password
$creds['user_login'] = "";
$creds['user_password'] = "";
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo $user->get_error_message();
} else {
wp_set_auth_cookie( $user->ID, true );
}
}
if ( !is_wp_error( $user ) ) {
// Success! We're logged in! Now let's test against EDD's purchase of my "service."
if ( edd_has_user_purchased( $user->ID, '294', NULL ) ) {
echo "Purchased the Services and is active.";
} else {
echo "Not Purchased";
}
}
?>
由于您使用的是简易数字下载请确保它已包含在内
if ( !function_exists( 'edd_has_user_purchased' ) ) {
require_once 'path-to-plugin/user-functions.php';
}
在if(!is_wp_error($user))语句中,您可以使用
echo "<p>ID: ".$user->ID;
echo "<p>Name: ".$user->data->display_name;
echo "<p>Login: ".$user->data->user_login;
echo "<p>Email: ".$user->data->user_email;
echo "<p>URL: ".$user->data->user_url;
echo "<p>Registered: ".$user->data->user_registered;
要查看所有可访问字段您确定wp-load.php的路径正确吗?除非您已经为此编辑了代码,require(“/var/www/yourdomain.com/htdocs/wp load.php”)
是一个通用路径,应该替换为实际的服务器路径(而不是URL路径)
print_r($user);