Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <?php
  2. // remember - the suffix of the hook should contain specific form id!
  3.  
  4. add_action( "gform_pre_submission_3", "wp_doin_pre_submission" );
  5.  
  6. function wp_doin_pre_submission($form) {
  7.  
  8. // get the user created arguments and store them in an array
  9. // each of the $_POST elements should be read from the form visual editor
  10. $username = esc_attr( $_POST['input_1'] );
  11. $email = sanitize_email( $_POST['input_2'] );
  12.  
  13. // if we're doing a cron job let's forget about it
  14. if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) )
  15. return;
  16.  
  17. // let's check if a user with given name exists
  18. // we're already doing that in the form validation, but this gives us another bridge of safety
  19. $user_id = username_exists( $username );
  20.  
  21. // let's validate the email and the user
  22. if ( !$user_id and email_exists( $email ) == false ) {
  23.  
  24. // let's generate a passowrd
  25. $random_password = wp_generate_password( $length = 12, false );
  26.  
  27. // let's create a new user
  28. $user_id = wp_create_user( $name, $random_password, $email );
  29.  
  30. // let's notify the new user
  31. wp_new_user_notification( $user_id, $random_password );
  32. } else {
  33. // validation failed
  34. return;
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement