Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create user by Perfil is published or updated.
- *
- * @param int $post_id The post ID.
- * @param post $post The post object.
- * @param bool $update Whether this is an existing post being updated or not.
- */
- function perfil_save_create_user( $post_id, $post, $update ) {
- $post_type = get_post_type( $post_id );
- if ( 'perfil' != $post_type ) return;
- $slug = $post->post_name;
- $title = $post->post_title;
- $email_address = get_post_meta( $post_id, 'email', true );
- if ( null == username_exists( $slug ) && !empty( $email_address ) ) {
- // Generate the password and create the user
- $password = wp_generate_password( 12, false );
- $user_id = wp_create_user( $slug, $password, $email_address );
- // Set fields of the user
- wp_update_user(
- array(
- 'ID' => $user_id,
- 'first_name' => $title,
- 'nickname' => $title,
- 'display_name' => $title
- )
- );
- // Set the role
- $user = new WP_User( $user_id );
- $user->set_role( 'perfil' );
- // Email the user
- //wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );
- // Set the ID of owner this Perfil
- update_post_meta( $post_id, 'user_owner', $user_id );
- // Set author of Perfil
- $arg = array(
- 'ID' => $post_id,
- 'post_author' => $user_id,
- );
- wp_update_post( $arg );
- }
- }
- add_action( 'save_post', 'perfil_save_create_user', 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement