Guest User

Untitled

a guest
Jan 30th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. [insert_php]
  2.  
  3. // Initialise variables
  4. $name = "";
  5. $password = "";
  6. $email = "";
  7. $phone = "";
  8. $how = "";
  9. $type = "";
  10.  
  11. // Sanitise fields
  12. if($_SERVER["REQUEST_METHOD"] == "POST")
  13. {
  14. $name = test_input($_POST["inputUsername"]);
  15. $password = test_input($_POST["inputPassword"]);
  16. $email = test_input($_POST["inputEmail"]);
  17. $phone = test_input($_POST["inputPhone"]);
  18. $how = test_input($_POST["inputHow"]);
  19. $type = test_input($_POST["inputType"]);
  20. }
  21.  
  22. // Validate fields
  23. registration_validation(
  24. $name,
  25. $password,
  26. $email,
  27. $phone,
  28. $how,
  29. $type
  30. );
  31.  
  32. // Attempt to submit to database.
  33. complete_registration(
  34. $name,
  35. $password,
  36. $email,
  37. $phone,
  38. $how,
  39. $type
  40. );
  41.  
  42. // Strip any special characters out of the input.
  43. function test_input($data)
  44. {
  45. $data = trim($data);
  46. $data = stripslashes($data);
  47. $data = htmlspecialchars($data);
  48. return $data;
  49. }
  50.  
  51. // Do specific checks e.g. length of username etc.
  52. function registration_validation($name, $password, $email, $phone, $how, $type)
  53. {
  54. global $reg_errors;
  55. $reg_errors = new WP_Error;
  56.  
  57. if (empty($name) || empty($password) || empty($email) || empty($phone) || empty($how) || empty($type))
  58. {
  59. $reg_errors->add('field', 'Required form field is missing');
  60. }
  61.  
  62. if (is_wp_error($reg_errors))
  63. {
  64. foreach ($reg_errors->get_error_messages() as $error)
  65. {
  66. echo '<div>';
  67. echo '<strong>ERROR</strong>:';
  68. echo $error . '<br/>';
  69. echo '</div>';
  70. }
  71. }
  72. }
  73.  
  74. // Submit to the database.
  75. function complete_registration($name, $password, $email, $phone, $how, $type)
  76. {
  77. global $reg_errors;
  78. if ( 1 > count( $reg_errors->get_error_messages() ) )
  79. {
  80. $userdata = array(
  81. 'user_login' => $name,
  82. 'user_email' => $email,
  83. 'user_pass' => $password,
  84. 'user_phone' => $phone,
  85. 'user_type' => $type,
  86. 'user_how' => $how
  87. );
  88. $user = wp_insert_user( $userdata );
  89. echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';
  90. }
  91. }
  92. [/insert_php]
  93.  
  94. <form method="post" action="[insert_php] echo htmlspecialchars($_SERVER["PHP_SELF"]);[/insert_php]">
  95. <div class="form-group">
  96. <label for="inputUsername">Username</label>
  97. <input type="text" class="form-control" id="inputUsername">
  98. </div>
  99. <div class="form-group">
  100. <label for="inputPassword">Password</label>
  101. <input type="password" class="form-control" id="inputPassword">
  102. </div>
  103. <div class="form-group">
  104. <label for="inputEmail">Email</label>
  105. <input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="name@example.com">
  106. </div>
  107. <div class="form-group">
  108. <label for="inputPhone">Contact Phone Number</label>
  109. <input type="password" class="form-control" id="inputPhone">
  110. </div>
  111. <div class="form-group">
  112. <label for="how">How did you hear about us?</label>
  113. <select class="form-control" id="how">
  114. <option>Word of mouth</option>
  115. <option>Local paper</option>
  116. <option>Search engine</option>
  117. <option>Other</option>
  118. </select>
  119. </div>
  120. <div class="form-group">
  121. <label for="signupType">I am a:</label>
  122. <select class="form-control" id="signupType">
  123. <option>Parent / Guardian</option>
  124. <option>Student</option>
  125. </select>
  126. </div>
  127. <button type="submit" value="Submit" class="btn btn-primary">Submit</button>
  128. </form>
Add Comment
Please, Sign In to add comment