WordPress POST操作为空

时间:2016-05-19 18:30:08

标签: wordpress forms get

我正在尝试为我的WordPress主题制作一个简单的登录表单。我认为我正确地遵循了WP文档,但我发现我的表单操作没有被检测到。

我可以在我的错误日志中看到'到这里1'正在打印。查看下面的process_form.php,此条件应指示我的用户未登录且该操作为空。

我相信我的表单处理代码中的这一行:

add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );

和我实际表格中的这一行

<input type="hidden" name="action" value="streamLogForm">

将进行交互以将表单操作绑定到此admin_post挂钩。

showroom_login_user()函数工作,我的GET变量实际上似乎正在工作(起初我以为它们不是),但仍不确定为什么我达到了这个条件。

的login.php

<head>
    <script type="text/javascript">     
        function validateLoginForm() {

            var email = document.forms["streamLogForm"]["user_email"].value;
            var password = document.forms["streamLogForm"]["user_password"].value;

            if ( email == null || email == "" ) { 
                alert("User must be entered"); 
                return false; 
            } else if ( password == null || password == "" ) {
                alert("Password must be entered"); 
                return false; 
            }

            return true;

        }

    </script>   
</head>

<div class="">

    <form name="streamLogForm"  id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="get" >

        <input type="hidden" name="action" value="streamLogForm">
        <input type="hidden" name="location" value="<?php echo $URI ?>" />
        <input type="hidden" name="action_type" value="<?php echo 'login'?>" />

        <div class="field">
            <label for="streamUserEmail">Email:</label>
            <input type="text" id="streamUserEmail" name="user_email">
        </div>

        <div class="field">
            <label for="streamUserPassword">Password: </label>
            <input type="password" id="streamUserEmail" name="user_password">
        </div>

        <button> Login </button>

    </form>

</div>

process_form.php

<?php

if ( ! wp_validate_auth_cookie() ) {
    if ( empty( $action ) ) { 
        error_log('got here 1', 0);  
        do_action( 'admin_post_nopriv' );
    } else { 
        error_log('got here 2', 0);
        do_action( "admin_post_nopriv_{$action}" );
    }
} else {
    if ( empty( $action ) ){ 
        error_log('got here 3', 0); 
        do_action( 'admin_post' ); 
    } else { 
        error_log('got here 4', 0); 
        do_action( "admin_post_{$action}" );
    }
}

function showroom_login_user() 
{
    error_log("GOT HERE 6!", 0);

    $location = $_GET["action_type"];
    $action_type = $_GET["action_type"];
    $user_email = $_GET["user_email"];
    $user_password = $_GET["user_password"];

    echo 'testing......' . $user_email;
}
add_action( 'admin_post_nopriv_streamLogForm', 'showroom_login_user' );
add_action( 'admin_post_streamLogForm', 'showroom_login_user' );

?>

1 个答案:

答案 0 :(得分:2)

像这样更新您的表单标签

<form name="streamLogForm"  id="streamLoginForm" onsubmit="return validateLoginForm()" action="<?php echo admin_url('admin-ajax.php'); ?>" method="get" >

并将此代码段放在functions.php

function showroom_login_user() {

    $creds = array();
    $creds['user_login'] = $_GET["user_email"];
    $creds['user_password'] = $_GET["user_password"];
    $creds['remember'] = true;

    $autologin_user = wp_signon( $creds, false );

    if ( !is_wp_error($autologin_user) ) {
        // "Logged In.!";
    } else {
        // "Log In Failed.!";
    }
    // do your redirect here
    wp_safe_redirect( "URL of the page to be redirected" );

}

add_action ( 'wp_ajax_quick_streamLogForm', 'showroom_login_user' );
add_action ( 'wp_ajax_nopriv_streamLogForm', 'showroom_login_user' );