验证WP以获取当前用户

时间:2018-03-01 05:30:59

标签: php wordpress authentication

我正在尝试将用户详细信息输出到word 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 ); // 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";
        }
    } 
?>

但它不起作用,我正在创建自定义仪表板输出wp,哪个用户wp信息作为后端。请告诉我,我在做什么错?任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

由于您使用 easy-digital-downloads ,请确保其已包含

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信息。

尝试

print_r($user); 

概述所有可访问的字段