自定义WordPress网站的自定义注册表单

时间:2017-08-17 作者:Jon_Snow

我正在建立一个音乐电子商务网站,出售一些音频。我已经建立了一个注册表格,有5个文本框,分别是姓名、电子邮件、密码、用户名和电话号码。我正在将此数据保存在wp注册表中。

我如何将此注册表格与网站链接,以便如果用户注册,那么只有他才能看到所有音乐内容,否则只能看到普通文本。

这是注册代码。

      <?php
      /* Template Name: Sign Up */
      get_header();
      $fNameErr  = $familyNameErr = $phoneErr = $emailErr = $passwordErr = "";
      $error_css = $fName = $familyName = $phone = $email = $password =  "";

      if (isset($_POST[\'submit\'])) {
      if (empty($_POST["fName"])) {
      $fNameErr = \'background-color:#ff4338\';

      } else {
      $fName = test_input($_POST["fName"]);
      // check if name only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z ]*$/", $fName)) {
        $fNameErr = "*";
      }
     } 



     if (empty($_POST["familyName"])) {
     $error_css = \'background-color:#ff4338\';
   } else {
     $familyName = test_input($_POST["familyName"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/", $familyName)) {
        $familyNameErr = "*";
      }
    }


    if (empty($_POST["email"])) {
    $error_css = \'background-color:#ff4338\';
    } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "Invalid email format";
    }
}



if (empty($_POST["phone"])) {
    $error_css = \'background-color:#ff4338\';
} else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phone)) {
        $phoneErr = "*";
    }
}



if (empty($_POST["password"])) {
    $error_css = \'background-color:#ff4338\';
} else {
    $password = test_input($_POST["password"]);
    // check if e-mail address is well-formed
}



global $wpdb;


$wpdb->insert(\'wp_registration\', array(
    \'First_Name\' => $_POST[\'fName\'],
    \'Last_Name\' => $_POST[\'familyName\'],
    \'Phone\' => $_POST[\'phone\'],
    \'Email\' => $_POST[\'email\'],
    \'Password\' => $_POST[\'password\']
), array(
    \'%s\',
    \'%s\'
));

}
?>


<form action="index.php" class="modal-content animate" method="post">
    <div class="container">
        <input id="fName" name="fName" onblur="this.placeholder = \'Prénom nom\'" onfocus="this.placeholder=\'\'" placeholder="Prénom nom" required="" style="<?php echo $fNameErr; ?>" type="text"> 
        <input name="familyName" onblur="this.placeholder = \'Nom de famille\'" onfocus="this.placeholder=\'\'" placeholder="Nom de famille" required="" style="<?php echo $error_css; ?>" type="text"> 
        <input name="phone" onblur="this.placeholder = \'Numéro de téléphone\'" onfocus="this.placeholder=\'\'" placeholder="Numéro de téléphone" required="" style="<?php echo $error_css; ?>" type="number"> 
        <input name="email" onblur="this.placeholder = \'Courriel\'" onfocus="this.placeholder=\'\'" placeholder="Courriel" required="" style="<?php echo $error_css; ?>" type="text">
        <input name="password" onblur="this.placeholder = \'Mot de passe\'" onfocus="this.placeholder=\'\'" placeholder="Mot de passe" required="" style="<?php echo $error_css; ?>" type="password"><br>

        <div class="buttons">
            <input class="btn btn-primary custom-button red-btn" name="submit" type="submit" value="Inscription">
        </div>
    </div>
</form>

1 个回复
SO网友:Nikita Dudarev

也许这个插件可以帮助您
https://wordpress.org/plugins/pagerestrict/
https://wordpress.org/plugins/wordpress-access-control/

Update

if ( is_user_logged_in() ) {
    // you content
}
else {
    // you text
}

UPDATE 2

当用户被授权时,him ID被写入全局数组。可以从此数组中获取此ID。

$current_user = wp_get_current_user();
我不知道你的表的结构,但你需要有用户ID字段,如wp_users 桌子向数据库发出请求。

global $wpdb

$value = $wpdb->get_result("SELECT `user_name_in_your_table`
                            FROM `wp_registration`
                            WHERE `user_id` = \'$current_user->ID\'");

if($value[0]->user_name_in_your_table !== \'\'){
    //access
} else{
    //denied
}

结束