Advertisement
Abubakar1309

Wp sign up

Sep 2nd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' ) {
  2. // We're registering
  3. }
  4.  
  5. add_action('register_form', 'register_message');
  6. function register_message() {
  7. $html = '
  8. <div style="margin:5px 0;border:1px solid red;padding:5px">
  9. <p style="margin:5px 0;">
  10. Hello Cruncher..!! Use this form to reigster at Crunchify.com.
  11. </p>
  12. </div>';
  13. echo $html;
  14. }
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. field to my form:
  22.  
  23. //Add Custom Password Field
  24. $output .= '<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">';
  25. $output .='<label class="password">';
  26.  
  27. $cs_opt_array = array(
  28. 'id' => '',
  29. 'std' => __('Password', 'jobhunt'),
  30. 'cust_id' => 'user_pass' . $rand_id,
  31. 'cust_name' => 'pass1',
  32. 'cust_type' => 'password',
  33. 'classes' => 'form-control',
  34. 'extra_atr' => ' size="100" tabindex="101" placeholder="' . __('Password*', 'jobhunt') . '"',
  35. 'return' => true,
  36.  
  37.  
  38. );
  39. $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);
  40.  
  41. $output .='</label>';
  42.  
  43. $output .= '</div>';
  44. $output .= '<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">';
  45. $output .='<label class="password">';
  46.  
  47. $cs_opt_array = array(
  48. 'id' => '',
  49. 'std' => __('Password', 'jobhunt'),
  50. 'cust_id' => 'user_pass' . $rand_id,
  51. 'cust_name' => 'pass2',
  52. 'cust_type' => 'password',
  53. 'classes' => 'form-control',
  54. 'extra_atr' => ' size="100" tabindex="101" placeholder="' . __('Repeat Password*', 'jobhunt') . '"',
  55. 'return' => true,
  56. );
  57. $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);
  58.  
  59. $output .='</label>';
  60. $output .= '</div>';
  61. //End
  62. After that i validate this two fields by below code:
  63.  
  64. if ( empty( $_POST['pass1'] ) || ! empty( $_POST['pass1'] ) && trim( $_POST['pass1'] ) == '' ) {
  65. $json['type'] = "error";
  66. $json['message'] = $cs_danger_html . __('<strong>ERROR</strong>: Password field is required.') . $cs_msg_html;
  67. echo json_encode($json);
  68. exit();
  69. }
  70. if ( empty( $_POST['pass2'] ) || ! empty( $_POST['pass2'] ) && trim( $_POST['pass2'] ) == '' ) {
  71. $json['type'] = "error";
  72. $json['message'] = $cs_danger_html . __('<strong>ERROR</strong>: Confirm Password field is required.') . $cs_msg_html;
  73. echo json_encode($json);
  74. exit();
  75. }
  76. if ( $_POST['pass1'] != $_POST['pass2'] ) {
  77. $json['type'] = "error";
  78. $json['message'] = $cs_danger_html . __('<strong>ERROR</strong>: Password field and Confirm Password field do not match.') . $cs_msg_html;
  79. echo json_encode($json);
  80. exit();
  81. }
  82.  
  83. $random_password = $_POST['pass1'];
  84. After validation its time to save the data in database and i used below code for saving the entered password:
  85.  
  86. function mytheme_registration_save($user_id) {
  87.  
  88.  
  89. if ( isset($_REQUEST['action']) && $_REQUEST['action'] == 'register' ) {
  90.  
  91. $random_password = $_POST['pass1'];
  92. wp_set_password($random_password, $user_id);
  93. $reg_user = get_user_by('ID', $user_id);
  94. if ( isset($reg_user->roles) && (in_array('subscriber', $reg_user->roles) || in_array('editor', $reg_user->roles) || in_array('author', $reg_user->roles)) ) {
  95. // Site owner email hook
  96. do_action('theme_domain_new_user_notification_site_owner', $reg_user->data->user_login, $reg_user->data->user_email);
  97. // normal user email hook
  98. do_action('theme_domain_user_register', $reg_user, $random_password);
  99. }
  100. }
  101. }
  102. }
  103.  
  104.  
  105.  
  106. functions.php
  107.  
  108. function wc_registration_redirect( $redirect_to ) {
  109. wp_logout();
  110. wp_redirect( '/sign-in/?q=');
  111. exit;
  112. }
  113. // when user login, we will check whether this guy email is verify
  114. function wp_authenticate_user( $userdata ) {
  115. $isActivated = get_user_meta($userdata->ID, 'is_activated', true);
  116. if ( !$isActivated ) {
  117. $userdata = new WP_Error(
  118. 'inkfool_confirmation_error',
  119. __( '<strong>ERROR:</strong> Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u='.$userdata->ID.'">here</a>', 'inkfool' )
  120. );
  121. }
  122. return $userdata;
  123. }
  124. // when a user register we need to send them an email to verify their account
  125. function my_user_register($user_id) {
  126. // get user data
  127. $user_info = get_userdata($user_id);
  128. // create md5 code to verify later
  129. $code = md5(time());
  130. // make it into a code to send it to user via email
  131. $string = array('id'=>$user_id, 'code'=>$code);
  132. // create the activation code and activation status
  133. update_user_meta($user_id, 'is_activated', 0);
  134. update_user_meta($user_id, 'activationcode', $code);
  135. // create the url
  136. $url = get_site_url(). '/sign-in/?p=' .base64_encode( serialize($string));
  137. // basically we will edit here to make this nicer
  138. $html = 'Please click the following links <br/><br/> <a href="'.$url.'">'.$url.'</a>';
  139. // send an email out to user
  140. wc_mail($user_info->user_email, __('Please activate your account'), $html);
  141. }
  142. // we need this to handle all the getty hacks i made
  143. function my_init(){
  144. // check whether we get the activation message
  145. if(isset($_GET['p'])){
  146. $data = unserialize(base64_decode($_GET['p']));
  147. $code = get_user_meta($data['id'], 'activationcode', true);
  148. // check whether the code given is the same as ours
  149. if($code == $data['code']){
  150. // update the db on the activation process
  151. update_user_meta($data['id'], 'is_activated', 1);
  152. wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! ', 'inkfool' ) );
  153. }else{
  154. wc_add_notice( __( '<strong>Error:</strong> Activation fails, please contact our administrator. ', 'inkfool' ) );
  155. }
  156. }
  157. if(isset($_GET['q'])){
  158. wc_add_notice( __( '<strong>Error:</strong> Your account has to be activated before you can login. Please check your email.', 'inkfool' ) );
  159. }
  160. if(isset($_GET['u'])){
  161. my_user_register($_GET['u']);
  162. wc_add_notice( __( '<strong>Succes:</strong> Your activation email has been resend. Please check your email.', 'inkfool' ) );
  163. }
  164. }
  165. // hooks handler
  166. add_action( 'init', 'my_init' );
  167. add_filter('woocommerce_registration_redirect', 'wc_registration_redirect');
  168. add_filter('wp_authenticate_user', 'wp_authenticate_user',10,2);
  169. add_action('user_register', 'my_user_register',10,2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement