Guest User

Untitled

a guest
Feb 5th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. <?php
  2.  
  3. require_once( ABSPATH . '/wp-includes/shortcodes.php' );
  4.  
  5.  
  6. function custom_registration_function() {
  7. if (isset($_POST['submit'])) {
  8. registration_validation(
  9. $_POST['username'],
  10. $_POST['password'],
  11. $_POST['email'],
  12. $_POST['website'],
  13. $_POST['fname'],
  14. $_POST['lname'],
  15. $_POST['nickname'],
  16. $_POST['bio']
  17. );
  18.  
  19. // sanitize user form input
  20. global $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
  21. $username = sanitize_user($_POST['username']);
  22. $password = esc_attr($_POST['password']);
  23. $email = sanitize_email($_POST['email']);
  24. $website = esc_url($_POST['website']);
  25. $first_name = sanitize_text_field($_POST['fname']);
  26. $last_name = sanitize_text_field($_POST['lname']);
  27. $nickname = sanitize_text_field($_POST['nickname']);
  28. $bio = esc_textarea($_POST['bio']);
  29.  
  30. // call @function complete_registration to create the user
  31. // only when no WP_error is found
  32. complete_registration(
  33. $username,
  34. $password,
  35. $email,
  36. $website,
  37. $first_name,
  38. $last_name,
  39. $nickname,
  40. $bio
  41. );
  42. }
  43.  
  44. registration_form(
  45. $username,
  46. $password,
  47. $email,
  48. $website,
  49. $first_name,
  50. $last_name,
  51. $nickname,
  52. $bio
  53. );
  54. }
  55.  
  56. function registration_form( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
  57. echo '
  58. <style>
  59. div {
  60. margin-bottom:2px;
  61. }
  62.  
  63. input{
  64. margin-bottom:4px;
  65. }
  66. </style>
  67. ';
  68.  
  69. echo '
  70. <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
  71. <div>
  72. <label for="username">Username <strong>*</strong></label>
  73. <input type="text" name="username" value="' . (isset($_POST['username']) ? $username : null) . '">
  74. </div>
  75.  
  76. <div>
  77. <label for="password">Password <strong>*</strong></label>
  78. <input type="password" name="password" value="' . (isset($_POST['password']) ? $password : null) . '">
  79. </div>
  80.  
  81. <div>
  82. <label for="email">Email <strong>*</strong></label>
  83. <input type="text" name="email" value="' . (isset($_POST['email']) ? $email : null) . '">
  84. </div>
  85.  
  86. <div>
  87. <label for="website">Website</label>
  88. <input type="text" name="website" value="' . (isset($_POST['website']) ? $website : null) . '">
  89. </div>
  90.  
  91. <div>
  92. <label for="firstname">First Name</label>
  93. <input type="text" name="fname" value="' . (isset($_POST['fname']) ? $first_name : null) . '">
  94. </div>
  95.  
  96. <div>
  97. <label for="website">Last Name</label>
  98. <input type="text" name="lname" value="' . (isset($_POST['lname']) ? $last_name : null) . '">
  99. </div>
  100.  
  101. <div>
  102. <label for="nickname">Nickname</label>
  103. <input type="text" name="nickname" value="' . (isset($_POST['nickname']) ? $nickname : null) . '">
  104. </div>
  105.  
  106. <div>
  107. <label for="bio">About / Bio</label>
  108. <textarea name="bio">' . (isset($_POST['bio']) ? $bio : null) . '</textarea>
  109. </div>
  110. <input type="submit" name="submit" value="Register"/>
  111. </form>
  112. ';
  113. }
  114.  
  115. function registration_validation( $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio ) {
  116. global $reg_errors;
  117. $reg_errors = new WP_Error;
  118.  
  119. if ( empty( $username ) || empty( $password ) || empty( $email ) ) {
  120. $reg_errors->add('field', 'Required form field is missing');
  121. }
  122.  
  123. if ( strlen( $username ) < 4 ) {
  124. $reg_errors->add('username_length', 'Username too short. At least 4 characters is required');
  125. }
  126.  
  127. if ( username_exists( $username ) )
  128. $reg_errors->add('user_name', 'Sorry, that username already exists!');
  129.  
  130. if ( !validate_username( $username ) ) {
  131. $reg_errors->add('username_invalid', 'Sorry, the username you entered is not valid');
  132. }
  133.  
  134. if ( strlen( $password ) < 5 ) {
  135. $reg_errors->add('password', 'Password length must be greater than 5');
  136. }
  137.  
  138. if ( !is_email( $email ) ) {
  139. $reg_errors->add('email_invalid', 'Email is not valid');
  140. }
  141.  
  142. if ( email_exists( $email ) ) {
  143. $reg_errors->add('email', 'Email Already in use');
  144. }
  145.  
  146. if ( !empty( $website ) ) {
  147. if ( !filter_var($website, FILTER_VALIDATE_URL) ) {
  148. $reg_errors->add('website', 'Website is not a valid URL');
  149. }
  150. }
  151.  
  152. if ( is_wp_error( $reg_errors ) ) {
  153.  
  154. foreach ( $reg_errors->get_error_messages() as $error ) {
  155. echo '<div>';
  156. echo '<strong>ERROR</strong>:';
  157. echo $error . '<br/>';
  158.  
  159. echo '</div>';
  160. }
  161. }
  162. }
  163.  
  164. function complete_registration() {
  165. global $reg_errors, $username, $password, $email, $website, $first_name, $last_name, $nickname, $bio;
  166. if ( count($reg_errors->get_error_messages()) < 1 ) {
  167. $userdata = array(
  168. 'user_login' => $username,
  169. 'user_email' => $email,
  170. 'user_pass' => $password,
  171. 'user_url' => $website,
  172. 'first_name' => $first_name,
  173. 'last_name' => $last_name,
  174. 'nickname' => $nickname,
  175. 'description' => $bio,
  176. );
  177. $user = wp_insert_user( $userdata );
  178. echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';
  179. }
  180. }
  181.  
  182. // Register a new shortcode: [cr_custom_registration]
  183. add_shortcode('cr_custom_registration', 'custom_registration_shortcode');
  184.  
  185. // The callback function that will replace [book]
  186. function custom_registration_shortcode() {
  187. ob_start();
  188. custom_registration_function();
  189. return ob_get_clean();
  190. }
Add Comment
Please, Sign In to add comment