使用wordpress登录功能

Sba*_*bad 5 php wordpress customization

有没有人有使用这些功能编写自定义Wordpress登录页面的经验:

wp_signon()
and wp_set_auth_cookie()
Run Code Online (Sandbox Code Playgroud)

http://codex.wordpress.org/Function_Reference/上找到

我似乎无法让他们工作.

代码看起来像这样:

function login_wordpress($username, $password) {
    $creds = array();
    $creds['user_login'] = $username;
    $creds['user_password'] = $password;
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ) {
       echo $user->get_error_message();
       die();
    } else {
         wp_set_auth_cookie( $user, 0, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么基本的东西?

小智 7

您需要更改此行:

wp_set_auth_cookie( $user, 0, 0);
Run Code Online (Sandbox Code Playgroud)

对此:

wp_set_auth_cookie( $user->ID, 0, 0);
Run Code Online (Sandbox Code Playgroud)

$userWP_UserObject而不是用户ID.

wp_signonWP_Error失败或WP_User成功时返回.


arl*_*dia -1

几年前我在一个项目中做到了这一点,所以 WordPress 代码有点不同。但这段代码对我有用:

// include the wordpress files necessary to run its functions
include('../classpages/wp-config.php'); // this includes wp-settings.php, which includes wp-db.php, which makes the database connection
include(ABSPATH . WPINC . '/pluggable-functions.php');

// use wordpress's function to create the login cookies
// this creates a cookie for the username and another for the hashed password, which wordpress reauthenticates each time we call its wp_get_current_user() function
wp_setcookie($user_login, $user_pass, false, '', '', $cookieuser);
Run Code Online (Sandbox Code Playgroud)

我根本不必使用 wp_signon,但这可能已经改变了。

您是否收到错误消息,或者运行代码时看到了什么?