firoze

custom wordpress register form by shortcode

Jul 23rd, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. // wordpress register form
  2.  
  3. // functions.php
  4.  
  5. class jeba_registration_form
  6. {
  7. private $username;
  8. private $email;
  9. private $password;
  10. private $website;
  11. private $first_name;
  12. private $last_name;
  13. private $nickname;
  14. private $bio;
  15. function __construct()
  16. {
  17. add_shortcode('jeba_registration_form', array($this, 'shortcode'));
  18. add_action('wp_enqueue_scripts', array($this, 'flat_ui_kit'));
  19. }
  20. public function registration_form()
  21. {
  22. ?>
  23. <form method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
  24. <div class="login-form">
  25. <div class="form-group">
  26. <input name="reg_name" type="text" class="form-control login-field"
  27. value="<?php echo(isset($_POST['reg_name']) ? $_POST['reg_name'] : null); ?>"
  28. placeholder="Username" id="reg-name" required/>
  29. <label class="login-field-icon fui-user" for="reg-name"></label>
  30. </div>
  31. <div class="form-group">
  32. <input name="reg_email" type="email" class="form-control login-field"
  33. value="<?php echo(isset($_POST['reg_email']) ? $_POST['reg_email'] : null); ?>"
  34. placeholder="Email" id="reg-email" required/>
  35. <label class="login-field-icon fui-mail" for="reg-email"></label>
  36. </div>
  37. <div class="form-group">
  38. <input name="reg_password" type="password" class="form-control login-field"
  39. value="<?php echo(isset($_POST['reg_password']) ? $_POST['reg_password'] : null); ?>"
  40. placeholder="Password" id="reg-pass" required/>
  41. <label class="login-field-icon fui-lock" for="reg-pass"></label>
  42. </div>
  43. <div class="form-group">
  44. <input name="reg_website" type="text" class="form-control login-field"
  45. value="<?php echo(isset($_POST['reg_website']) ? $_POST['reg_website'] : null); ?>"
  46. placeholder="Website" id="reg-website"/>
  47. <label class="login-field-icon fui-chat" for="reg-website"></label>
  48. </div>
  49. <div class="form-group">
  50. <input name="reg_fname" type="text" class="form-control login-field"
  51. value="<?php echo(isset($_POST['reg_fname']) ? $_POST['reg_fname'] : null); ?>"
  52. placeholder="First Name" id="reg-fname"/>
  53. <label class="login-field-icon fui-user" for="reg-fname"></label>
  54. </div>
  55. <div class="form-group">
  56. <input name="reg_lname" type="text" class="form-control login-field"
  57. value="<?php echo(isset($_POST['reg_lname']) ? $_POST['reg_lname'] : null); ?>"
  58. placeholder="Last Name" id="reg-lname"/>
  59. <label class="login-field-icon fui-user" for="reg-lname"></label>
  60. </div>
  61. <div class="form-group">
  62. <input name="reg_nickname" type="text" class="form-control login-field"
  63. value="<?php echo(isset($_POST['reg_nickname']) ? $_POST['reg_nickname'] : null); ?>"
  64. placeholder="Nickname" id="reg-nickname"/>
  65. <label class="login-field-icon fui-user" for="reg-nickname"></label>
  66. </div>
  67. <div class="form-group">
  68. <input name="reg_bio" type="text" class="form-control login-field"
  69. value="<?php echo(isset($_POST['reg_bio']) ? $_POST['reg_bio'] : null); ?>"
  70. placeholder="About / Bio" id="reg-bio"/>
  71. <label class="login-field-icon fui-new" for="reg-bio"></label>
  72. </div>
  73. <input class="btn btn-primary btn-lg btn-block" type="submit" name="reg_submit" value="Register"/>
  74. </form>
  75. </div>
  76. <?php
  77. }
  78. function validation()
  79. {
  80. if (empty($this->username) || empty($this->password) || empty($this->email)) {
  81. return new WP_Error('field', 'Required form field is missing');
  82. }
  83. if (strlen($this->username) < 4) {
  84. return new WP_Error('username_length', 'Username too short. At least 4 characters is required');
  85. }
  86. if (strlen($this->password) < 5) {
  87. return new WP_Error('password', 'Password length must be greater than 5');
  88. }
  89. if (!is_email($this->email)) {
  90. return new WP_Error('email_invalid', 'Email is not valid');
  91. }
  92. if (email_exists($this->email)) {
  93. return new WP_Error('email', 'Email Already in use');
  94. }
  95. if (!empty($website)) {
  96. if (!filter_var($this->website, FILTER_VALIDATE_URL)) {
  97. return new WP_Error('website', 'Website is not a valid URL');
  98. }
  99. }
  100. $details = array('Username' => $this->username,
  101. 'First Name' => $this->first_name,
  102. 'Last Name' => $this->last_name,
  103. 'Nickname' => $this->nickname,
  104. 'bio' => $this->bio
  105. );
  106. foreach ($details as $field => $detail) {
  107. if (!validate_username($detail)) {
  108. return new WP_Error('name_invalid', 'Sorry, the "' . $field . '" you entered is not valid');
  109. }
  110. }
  111. }
  112. function registration()
  113. {
  114. $userdata = array(
  115. 'user_login' => esc_attr($this->username),
  116. 'user_email' => esc_attr($this->email),
  117. 'user_pass' => esc_attr($this->password),
  118. 'user_url' => esc_attr($this->website),
  119. 'first_name' => esc_attr($this->first_name),
  120. 'last_name' => esc_attr($this->last_name),
  121. 'nickname' => esc_attr($this->nickname),
  122. 'description' => esc_attr($this->bio),
  123. );
  124. if (is_wp_error($this->validation())) {
  125. echo '<div style="margin-bottom: 6px" class="btn btn-block btn-lg btn-danger">';
  126. echo '<strong>' . $this->validation()->get_error_message() . '</strong>';
  127. echo '</div>';
  128. } else {
  129. $register_user = wp_insert_user($userdata);
  130. if (!is_wp_error($register_user)) {
  131. echo '<div style="margin-bottom: 6px" class="btn btn-block btn-lg btn-danger">';
  132. echo '<strong>Registration complete. Goto <a href="' . wp_login_url() . '">login page</a></strong>';
  133. echo '</div>';
  134. } else {
  135. echo '<div style="margin-bottom: 6px" class="btn btn-block btn-lg btn-danger">';
  136. echo '<strong>' . $register_user->get_error_message() . '</strong>';
  137. echo '</div>';
  138. }
  139. }
  140. }
  141. function flat_ui_kit()
  142. {
  143. wp_register_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css', array('jquery'), 3.3, true);
  144. wp_enqueue_script('bootstrap');
  145. }
  146. function shortcode()
  147. {
  148. ob_start();
  149. if (isset($_POST['reg_submit']) ? $_POST['reg_submit'] : null) {
  150. $this->username = $_POST['reg_name'];
  151. $this->email = $_POST['reg_email'];
  152. $this->password = $_POST['reg_password'];
  153. $this->website = $_POST['reg_website'];
  154. $this->first_name = $_POST['reg_fname'];
  155. $this->last_name = $_POST['reg_lname'];
  156. $this->nickname = $_POST['reg_nickname'];
  157. $this->bio = $_POST['reg_bio'];
  158. $this->validation();
  159. $this->registration();
  160. }
  161. $this->registration_form();
  162. return ob_get_clean();
  163. }
  164. }
  165. new jeba_registration_form;
  166.  
  167.  
  168. ********************************************************************************************************
  169. // show in template this shortcode
  170.  
  171. <?php echo do_shortcode('[jeba_registration_form]');?>
Advertisement
Add Comment
Please, Sign In to add comment