创建pagenot的Wordpress注册即将发送到表单提交

时间:2017-04-23 13:22:05

标签: php wordpress forms

我创建了一个创建登录注册表单自定义的功能,但我不知道为什么当我点击提交时它显示我404未找到错误我不知道为什么会发生这种情况

unction registration_form( $name, $fname, $email, $password, $familyname, $username,  $password, $gender) {

echo '
<div class="login-form">
<form action="' . get_site_url() . '/sign-up" method="post">
<div class="form-group">
<input type="text" name="name" placeholder="Name" value="' . ( isset( $_POST['name'] ) ? $name : null ) . '">
</div>

<div class="form-group">
<input type="text" name="fname" placeholder="Father Name" value="' . ( isset( $_POST['fname']) ? $fname : null ) . '">
</div>

<div class="form-group">
<input type="text" name="familyname" placeholder="Family Name" value="' . ( isset( $_POST['familyname']) ? $familyname : null ) . '">
</div>

<div class="form-group">
<input type="text" name="email" placeholder="Email" value="' . ( isset( $_POST['email']) ? $email : null ) . '">
</div>

<div class="form-group">
<input type="text" name="username" placeholder="Username" value="' . ( isset( $_POST['username']) ? $username : null ) . '">
</div>

<div class="form-group">
<input type="password" name="password" placeholder="Password" value="' . ( isset( $_POST['password'] ) ? $password : null ) . '">
</div>

<div class="form-group">
    <label for="nickname">Gender</label>
    <div><input type="radio" name="gender" value="Male"> Male</div> 
    <div><input type="radio" name="gender" value="Female"> Female</div> 
</div>

<input type="submit" name="submit" value="Register"/>

<div class="form-group"><br>
Already have account with us <a href="'.get_site_url().'/log-in" title="" class="reg" id="">Login Here</a>
</div>

</form>
</div>
';
 }

function complete_registration() {
global $name, $fname, $password, $familyname, $username,  $password, $gender;
if ( 1 > count( $reg_errors->get_error_messages() ) ) {
    $userdata = array(
    'user_login'    =>   $username,
    'user_email'    =>   $email,
    'user_pass'     =>   $password,
    'user_name'     =>   $name,
    'family_name'   =>   $familyname,
    'father_name'   =>   $fname,
    'gender'        =>   $gender
    );
    $user = wp_insert_user( $userdata );
    echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';   
}

}

function custom_registration_function() {
if ( isset($_POST['submit'] ) ) {
    // sanitize user form input
    $username   =   sanitize_user( $_POST['username'] );
    $password   =   esc_attr( $_POST['password'] );
    $email      =   sanitize_email( $_POST['email'] );
    $name       =   esc_url( $_POST['name'] );
    $fname      =   sanitize_text_field( $_POST['fname'] );
    $familyname =   sanitize_text_field( $_POST['familyname'] );
    $gender     =   sanitize_text_field( $_POST['gender'] );

    // call @function complete_registration to create the user
    // only when no WP_error is found
    complete_registration(
        $username,
        $password,
        $email,
        $name,
        $fname,
        $familyname,
        $gender
    );
} else {
     registration_form(
       $username,
        $password,
        $email,
        $name,
        $fname,
        $familyname,
        $gender
    );
}

}

// Register a new shortcode: [cr_custom_registration]
add_shortcode( 'cr_custom_registration', 'custom_registration_shortcode' );

// The callback function that will replace [book]
function custom_registration_shortcode() {
    ob_start();
    custom_registration_function();
    return ob_get_clean();
}

以上是我创建的代码,但是当我点击提交时,它显示我找不到404因为它没有被发现该动作中给出的链接是相同的所以我不知道为什么会发生这种情况

1 个答案:

答案 0 :(得分:1)

你得到404,因为你的表单行为是“注册”,我认为你没有创建这样的网址。您需要创建一个显示自定义表单的页面模板,并将其分配给具有“注册”slug的页面。或者,您可以创建一个虚拟页面,您应该在其中处理表单。

有关如何在wordpress中创建虚拟页面的详细信息:https://wordpress.stackexchange.com/questions/9870/how-do-you-create-a-virtual-page-in-wordpress