前端登录表单在现场不起作用

时间:2013-03-15 作者:Adi

我正在使用前端表单提交。它在localhost上运行得很好,但当我的站点处于活动状态时就不起作用了。它显示“成功消息”,但最初未登录。这是我的代码:

if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == \'log-in\' ) :    
    global $error;
    $login = wp_login( $_POST[\'user-name\'], $_POST[\'password\'] );
    $login = wp_signon( array( \'user_login\' => $_POST[\'user-name\'], \'user_password\' => $_POST[\'password\'], \'remember\' => $_POST[\'remember-me\'] ), false );

    if (!$error) {
        save_message( \'success\', __( \'You have successfully Login.\', \'frontendprofile\' ) );
        unset($_POST);
        wp_redirect( get_permalink( 4 ) );  
    }
    // wp_redirect( home_url() );
endif;
那么我该怎么做才能解决这个问题呢?

2 个回复
SO网友:Vinod Dalvi

不要使用wp\\u登录功能,因为它已被弃用,请使用wp_signon.wp\\u signon函数在失败时返回wp\\u Error,因此使用它来评估条件,如下所示。

<?php
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == \'log-in\' ) :

$login = wp_signon( array( \'user_login\' => $_POST[\'user-name\'], \'user_password\' => $_POST[\'password\'], \'remember\' => $_POST[\'remember-me\'] ), false );

if ( !is_wp_error($login) ){ 
save_message( \'success\', __( \'You have successfully Login.\', \'frontendprofile\' ) );
unset($_POST);
wp_redirect( get_permalink( 4 ) );
}

endif;
?>

SO网友:revo

您需要设置cookie,我对您的代码进行了如下修改:

Updated

<?php
function parse_user($info = null, $return = \'object\') {
    if ( is_null( $info ) ) {
        global $current_user;
        if ( empty( $current_user->ID ) ) return null;
        $info = get_userdata( $current_user->ID );
    }
    elseif ( empty( $info ) ) {
        return null;
    }
    if( $return == \'ID\' ) {
        if ( is_object( $info ) ) return $info->ID;
        if ( is_numeric( $info ) ) return $info;
    }
    elseif( $return == \'object\' ) {
        if ( is_object( $info ) && $info->ID) return $info;
        if ( is_object( $info )) return get_userdata( $info->ID );
        if ( is_numeric( $info ) ) return get_userdata( $info );
        if ( is_string( $info ) ) return get_userdatabylogin( $info );
    }   
    else {
        return null;
    }
}

function wp_login_user_po9856($username, $password, $remember) {
    // Get the user based on the username from the POST
    $user = parse_user($username);

    // Remove html tags from the variables
    $username = strip_tags($username);
    $password = strip_tags($password);

    // Validate the Form Data
    if(!wp_check_password( $password_stripped, $user->user_pass ) ) return new WP_Error(\'incorrect_password\', "Wrong Password!");

    // User login
    $login = wp_signon(array(\'user_login\' => $username, \'user_password\' => $password, \'remember\' => (bool)$remember));
    if (is_wp_error($login))
        return $login;

    // Unset
    unset($_POST);

    // Custom Message
    save_message( \'success\', __( \'You have successfully Login.\', \'frontendprofile\' ) );

    // Redirection
    wp_redirect(get_permalink( 4 ));
}
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && @$_POST[\'action\'] == \'log-in\')
{
    wp_login_user_po9856($_POST[\'user-name\'], $_POST[\'password\'], $_POST[\'remember-me\']);
}
?>

结束

相关推荐

Front-End Post Submission

我正在尝试添加一个表单,用户可以从前端提交帖子。我正在学习本教程:http://wpshout。com/wordpress从前端提交帖子/我正在做的是添加this code 到我的一个页面模板。表单显示正常,但当我单击“提交”按钮时,它会显示“Page not found error“”许多评论者说这不起作用。谁能给我指出正确的方向吗?代码是否不完整?有缺陷吗?我做错什么了吗?谢谢Towfiq I。