Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /**
  2. * Create user by Perfil is published or updated.
  3. *
  4. * @param int $post_id The post ID.
  5. * @param post $post The post object.
  6. * @param bool $update Whether this is an existing post being updated or not.
  7. */
  8. function perfil_save_create_user( $post_id, $post, $update ) {
  9.  
  10. $post_type = get_post_type( $post_id );
  11.  
  12. if ( 'perfil' != $post_type ) return;
  13.  
  14. $slug = $post->post_name;
  15. $title = $post->post_title;
  16. $email_address = get_post_meta( $post_id, 'email', true );
  17.  
  18. if ( null == username_exists( $slug ) && !empty( $email_address ) ) {
  19.  
  20. // Generate the password and create the user
  21. $password = wp_generate_password( 12, false );
  22. $user_id = wp_create_user( $slug, $password, $email_address );
  23.  
  24. // Set fields of the user
  25. wp_update_user(
  26. array(
  27. 'ID' => $user_id,
  28. 'first_name' => $title,
  29. 'nickname' => $title,
  30. 'display_name' => $title
  31. )
  32. );
  33.  
  34. // Set the role
  35. $user = new WP_User( $user_id );
  36. $user->set_role( 'perfil' );
  37.  
  38. // Email the user
  39. //wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
  40.  
  41. // Set the ID of owner this Perfil
  42. update_post_meta( $post_id, 'user_owner', $user_id );
  43.  
  44. // Set author of Perfil
  45. $arg = array(
  46. 'ID' => $post_id,
  47. 'post_author' => $user_id,
  48. );
  49. wp_update_post( $arg );
  50.  
  51. }
  52.  
  53. }
  54. add_action( 'save_post', 'perfil_save_create_user', 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement