-1

I am totally new to PHP. I am trying to automatically create a WordPress user when a form is submitted with a custom plugin using the following code:

add_action( 'gform_post_process', 'wp_create_user', 10, 3 );
function wp_create_user( $username, $random_password, $email ) {
    $user_login = wp_slash( $entry[1]);
    $user_email = wp_slash( $entry[2]);
    $user_pass = wp_generate_password( $length = 12, $include_standard_special_chars = false );
    $role = 'Cp Client';

    $userdata = compact( 'user_login', 'user_email', 'user_pass' );
    return wp_insert_user( $userdata );
}}

I have also tried with gform_after_submission and changing the function's name, but then my website breaks.

What Am I doing wrong? Is this even possible? Could someone offer me a code example, please?

Thanks in advance,

Paco

Paco
  • 3
  • 5
  • [`wp_create_user`](https://developer.wordpress.org/reference/functions/wp_create_user/) already exists in WordPress. You simply need to name your own function something different ... – CBroe Nov 14 '22 at 12:20
  • It clearly states in the error that your function name is already declared.. – FUZIION Nov 14 '22 at 12:21
  • @FUZIION So...? – Paco Nov 14 '22 at 13:52
  • @CBroe thanks for your answer! Only changing the name of the function should it work? My website still breaks. I receive the following error: A critical error has occurred on this site. – Paco Nov 14 '22 at 14:15
  • @FUZIION In my original post, I explained that I tried something in that regard as well as I do now. I also say I am new to PHP. So, your comment does not help. – Paco Nov 14 '22 at 16:20
  • Does this answer your question? [WP - wp\_create\_user() inside transition\_post\_status hook](https://stackoverflow.com/questions/61203170/wp-wp-create-user-inside-transition-post-status-hook) – FUZIION Nov 14 '22 at 16:27
  • This question explains how you programmatically register a user. – FUZIION Nov 14 '22 at 16:28
  • Hi @FUZIION. Thanks for your help. Your link does so, but when creating a post... Rochelle's answer was closer to what I was looking for. I have slightly modified it and now it is worked. You can see the final code in the last answer of this post. Thanks for your comments :) – Paco Nov 16 '22 at 12:34

2 Answers2

0

You can try something like the following. (You may want to add a first, last and display name as well.) There is some more info here: https://usersinsights.com/create-wordpress-users-programmatically/

add_action( 'gform_after_submission_185', 'register_a_user', 10, 2 );//replace 185 with the your form id
function register_a_user($entry, $form ){
       $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
       $user_id = wp_insert_user( array(
          'user_login' => rgar( $entry, '1' ),
          'user_pass' => substr(str_shuffle($chars),0,12),
          'user_email' => rgar( $entry, '3' ),
          'role' => 'cp-client'//check the slug name of the role
       ));
       return;
    }
Rochelle
  • 513
  • 1
  • 4
  • 8
  • Hi Rochelle, I am sorry but with this code, I receive the same error: cannot redeclare the function. I am going to try with the link that FUZIION sent me. – Paco Nov 16 '22 at 11:14
  • I have slightly modified your code, and now is working. Thanks a lot. Answer accepted. – Paco Nov 16 '22 at 12:29
0

With the help of Rochelle, I have come with a code that finally works. This is it:

add_action( 'gform_after_submission', 'create', 10, 2 );
function create( $entry, $form ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $user_login = wp_slash( $entry[3] );
    $user_email = wp_slash( $entry[2] );
    $user_pass  = substr(str_shuffle($chars),0,12);

    $userdata = compact( 'user_login', 'user_email', 'user_pass' );
    return wp_insert_user( $userdata );
}

Thanks to everyone who left a comment on this post!

Paco
  • 3
  • 5