Created
February 6, 2017 11:53
-
-
Save everaldomatias/ce2c98475f3d718b0ec1db4c38f42ea6 to your computer and use it in GitHub Desktop.
Função para criar usuário a partir de um custom post publicado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment