我有一个应用程序,我正在尝试访问一个需要登录的安全Wordpress网页。我在Wordpress codex上找到了一些我认为可以强制登录的代码,但它似乎什么也没做。我希望在用户不存在的情况下,我可以强制登录。
function custom_login() {
$creds = array();
$creds['user_login'] = 'myusername';
$creds['user_password'] = 'mypassword';
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
}
function download_page()
{
global $wpdb;
$user = wp_get_current_user();
if (!($user->exists())) {
custom_login();
}else{
return '<p>You\'re not logged in! <br /> <a style="color:#ffa200;" href="'. get_page_link(39) . '">Click here </a>' .
'to login and download products that you\'ve purchased.';
}
}发布于 2016-11-22 17:32:39
尝试以下代码。在你的functions.php中写入
function custom_login() {
$creds = array();
$creds['user_login'] = 'myusername';
$creds['user_password'] = 'mypassword';
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( !is_wp_error( $user ) ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
if($user->ID == 0) {
echo 'faild login';
} else {
echo 'success';
}
} else {
echo "The username and password you entered don't match.";
}
die();
}
add_action('wp_login', 'custom_login');https://stackoverflow.com/questions/40726450
复制相似问题