Home > OS >  Create Wordpress user when new custom post created
Create Wordpress user when new custom post created

Time:10-25

I want to create custom posts from frontend and backend. And while I create posts I want to create users. At frontend I use form and wp_insert_post so it creates post as I need. At backend I use this code:

add_action( 'publish_user_contact', 'custom_post_user_create', 1, 3);
function custom_post_user_create($post_id){

 $user_login_id = 'user_'.random_int(100000, 999999);    

 update_post_meta($post_id, 'idr_contact_user_id', $user_login_id); 

 $password = wp_generate_password( 10, true, true );

 if (!empty($_POST['contact_email'])){
  $userdata = [
    'user_login'      => "$user_login_id",
    'user_pass'       => "$password",     
    'user_email'      => $_POST['contact_email'],    
    'display_name'    => sanitize_text_field( $_POST['contact_first_name'] .' '. $_POST['contact_last_name'] ),     
    'first_name'      => sanitize_text_field( $_POST['contact_first_name'] ),
    'last_name'       => sanitize_text_field( $_POST['contact_last_name'] )   
  ];
} else {
  $userdata = [
    'user_login'      => "$user_login_id",
    'user_pass'       => "$password",  
    'display_name'    => sanitize_text_field( $_POST['contact_first_name'] .' '. $_POST['contact_last_name'] ),     
    'first_name'      => sanitize_text_field( $_POST['contact_first_name'] ),   
    'last_name'       => sanitize_text_field( $_POST['contact_last_name'] )   
  ];
}
$user_id = wp_insert_user( $userdata );
}

It creates user with custom post nut when I try to edit posts it creates more users. I tried to use new_to_publish, but it doesn't work (users don't creates). I tried to use save_post but it makes duplicates too.

How to prevent duplicates?

CodePudding user response:

Try adding a check for the custom field, then the code won't be executed when editing a post.

add_action( 'publish_user_contact', 'custom_post_user_create', 1, 3);
function custom_post_user_create($post_id){

 $user_login_id = 'user_'.random_int(100000, 999999); 

 if(get_post_meta($post_id, "idr_contact_user_id", true) ) {
    return;
 }

 update_post_meta($post_id, 'idr_contact_user_id', $user_login_id); 

 $password = wp_generate_password( 10, true, true );

 if (!empty($_POST['contact_email'])){
  $userdata = [
    'user_login'      => "$user_login_id",
    'user_pass'       => "$password",     
    'user_email'      => $_POST['contact_email'],    
    'display_name'    => sanitize_text_field( $_POST['contact_first_name'] .' '. $_POST['contact_last_name'] ),     
    'first_name'      => sanitize_text_field( $_POST['contact_first_name'] ),
    'last_name'       => sanitize_text_field( $_POST['contact_last_name'] )   
  ];
} else {
  $userdata = [
    'user_login'      => "$user_login_id",
    'user_pass'       => "$password",  
    'display_name'    => sanitize_text_field( $_POST['contact_first_name'] .' '. $_POST['contact_last_name'] ),     
    'first_name'      => sanitize_text_field( $_POST['contact_first_name'] ),   
    'last_name'       => sanitize_text_field( $_POST['contact_last_name'] )   
  ];
}
$user_id = wp_insert_user( $userdata );
}
  • Related