Skip Navigation

[Resolved] Create user account with cred post form informations

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 20 replies, has 2 voices.

Last updated by stephaneL-3 4 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1405027

Hi 🙂

Tell us what you are trying to do?
As per the subject line I am trying to create a User account on the fly (If not existing yet, if yes to attached to it) when submitting a post entry via a CRED Form. All fields for the user info are available in the CRED form already and I would like to use it to create the account (and send email notification to user if poss), or else if the Username is already registered to attach the submission to their profile.

Is there any documentation that you are following?
Yes and I also used the following post (https://toolset.com/forums/topic/create-user-account-with-cred-post-form-informations/) which kinda falls short towards the end. it is also dating back to 2015 so maybe something is different now?
Also I have gotten started using the Classified Ref Site as a base.

At the moment I have the following code int he funtion.php file:

/**
* Create User on Entry creation.
*/
add_action('cred_save_data', 'creation_client',10,2);
function creation_client($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==720)
{
if (isset($_POST['wpfc-identifiant']))
{
$password = wp_generate_password( 10, true, false );
$userData = array(
'user_login' => $_POST['wpfc-identifiant'],
'first_name' => $_POST['wpfc-nom-et-prenom'],
'user_pass' => $password,
'user_email' => $_POST['wpfc-email'],
'role' => 'advertiser'
);
wp_insert_user( $userData );

//On success
if ( ! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}
}
}
}
(I am not sure about the wpfc- prefix but since it wasn't doing anything at this stage I tried based on the post quoted above)

What is the link to your site?
hidden link
(it is still in development so not registered on this site yet as just staging, but I will when going live on the actual site)

Thank you I appreciate your time and assistance 🙂
Stephane

#1405223

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

That's an obvious problem, the prefix for Types custom fields is 'wpcf-' not 'wpfc-', so update that and then try again.

You would probably find it helpful to dump the $_POST object to the debug.log so you can inspect it and verify it contains what you expect.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

Update your code with messages to the debug.log such as...

error_log(print_r($_POST, true));

That might help you identify problems.

#1412461

Hi nigel,

Sorry for the delay, mad race down the finsih Xmas line :|.

Ok So I updated the function as advised and it works in creating the user and the post. However I still have an issue in that it won't allow for any redirection whether it is a message or URL redirect etc. the debug file doesn't help me either, no error there just a confirmation of the field submitted really. I hope you can point me in the right direction, as for now it submit the post/create the user but there is no confirmation that it has been successfully submitted.
Furthermore it would be great if there was also the User account creation confirmation email sent to the user submitting it, is it possible (same way as when a wp user is created?

here is the code I used in the function.php:

add_action('cred_save_data', 'creation_client',10,2);
function creation_client($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==720)
{
if (isset($_POST['wpcf-identifiant']))
{
$password = wp_generate_password( 10, true, false );
$userData = array(
'user_login' => $_POST['wpcf-identifiant'],
'first_name' => $_POST['wpcf-nom-et-prenom'],
'user_pass' => $password,
'user_email' => $_POST['wpcf-email'],
'role' => 'advertiser'
);
wp_insert_user( $userData );

error_log(print_r($_POST, true));

//On success
if ( ! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}
}
}
}

Additionaly I created an admin access for you if you need:

Thank you again for y our time, your assistance is greatly appreciated.
Cheers
Stephane

#1412867

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

When you say there is no success message or similar, there is nothing in your code that I see that would prevent the settings for your post form from working, which include displaying a message or redirecting to some page.

You've set the post form up to do something after the post has submitted?

If you want to send something like a registration email, the wp_insert_user function doesn't trigger such emails—as you've seen—but you can manually send an email from within the same code you have added to the cred_save_data hook using the wp_mail function: https://developer.wordpress.org/reference/functions/wp_mail/

#1412889

Hello nigel,
Thank you for your prompt reply.
Yes i set it to display a message, but also tried the url redirect w/o success.
I will try the user notification.
Cheers
Stephane

#1412893

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

If you disable the code you added, do your form settings work?

The code doesn't look problematic.

#1414505
2019-12-18_10-11-17.jpg

MMm sorry, but if I take it off it does show the message I set it to, see attached
s

#1415477

Hi Nigel,
A little update on this.
So I have managed to get the Form confirmation on-screen message working, as well as the User Account creation.

(just for info the issue was the following section in the fonction, which I commented out:
//On success
if ( ! is_wp_error( $user_id ) ) {
echo "User created : ". $user_id;
}

Now, almost there, if you could please assist me with this one last thing I can't figure out.
Now that the Cred Form creates the Post in the Toolset custom post type / Create the WP User based on the data from the Cred Form, I still can't work out how to assign the new User created based on the form to be the author of that Record just created, it remains blank when not logged in??

Again sorry for my newby'ness with this :-\ your help is most appreciated always 🙂

Cheers
Stephane

#1416493

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

The wp_insert_user functions returns the new user's ID, and you have the post ID of the post you want to assign this user as author, so you would do something like:

$user_id = wp_insert_user($userData);
wp_update_post( array( 'ID' => $post_id, 'post_author' => $user_id );
#1416627

Hi Nigel,

Thanks for your reply, I have added the code to the function, there was a missing parenthesis it seemed, but after adding it the post is still not assigned to newly created User? Did I not add it as it should be?

add_action('cred_save_data', 'creation_client',10,2);

function creation_client($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==3319)
{
if (isset($_POST['wpcf-identifiant']))
{
$password = wp_generate_password( 10, true, false );
$userData = array(
'user_login' => $_POST['wpcf-identifiant'],
'first_name' => $_POST['wpcf-nom-et-prenom'],
'user_pass' => $password,
'user_email' => $_POST['wpcf-email'],
'role' => 'advertiser'
);
wp_insert_user( $userData );

$user_id = wp_insert_user($userData);
wp_update_post( array('ID' => $post_id,'post_author' => $user_id));

error_log(print_r($_POST, true));

}
}
}

The Log reads the following error:

[19-Dec-2019 14:20:12 UTC] PHP Warning: mysqli_real_escape_string() expects parameter 2 to be string, object given in /home/medinsofiz/www/wp-includes/wp-db.php on line 1165
[19-Dec-2019 14:20:12 UTC] Array
(
[wpcf-le-pitch] => dgfhdfgadfgadf
[type-de-structure] => Array
(
[0] => 66
)

[type-de-structure_hierarchy] =>
[listing-category] => Array
(
[0] => 80
)

[listing-category_hierarchy] =>
[wpcf-annee-de-creation] => 3333
[wpcf-nombre-de-personnes] => 4
[wpcf-chiffre-d-affaires] => 6
[wpcf-site-internet] => hidden link
[wpcf-compte-twitter] =>
[wpcf-adresse-societe] => Humboldt 450, Buenos Aires, Argentina
[toolset-extended-form-wpcf-adresse-societe] => Array
(
[latitude] => -34.5945748
[longitude] => -58.447674500000005
)

[wpcf-listing-featured-image] => 8VU6Qm1T.jpg
[wpcf-galerie-photos] => Array
(
[0] =>
)

[wpcf-lien-video] =>
[wpcf-nom-et-prenom] => Stephane Lesca
[wpcf-fonction] => Company Name
[wpcf-identifiant] => stefanico2
[wpcf-email] => accounts@kustomdata.com
[wpcf-telephone] => +33615632091
[_cred_cred_prefix_post_id] => 3397
[_cred_cred_prefix_cred_container_id] => 3331
[_cred_cred_prefix_form_id] => 3319
[_cred_cred_prefix_form_count] => 1
[form_submit] => true
[action] => cred_submit_form
[wpnonce] => b1f2bff4e4
)

[19-Dec-2019 14:21:22 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'assign_any_author_func' not found or invalid function name in /home/medinsofiz/www/wp-includes/class-wp-hook.php on line 288
[19-Dec-2019 14:21:22 UTC] PHP Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'assign_any_author_func' not found or invalid function name in /home/medinsofiz/www/wp-includes/class-wp-hook.php on line 288

Thank you for your t ime
Stephane

#1416729

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

You shouldn't be inserting the user twice, I meant for you to replace the wp_insert_user

You have

wp_insert_user( $userData );

$user_id = wp_insert_user($userData);

It wouldn't be a bad idea after that to print $user_id to the error log to confirm it has returned the new user id.

#1416935
2019-12-19_20-00-42.png
Live Site -  missing edit button.png
Staging Site -  with edit button.png

YEAH 🙂 thank you so much all working perfect at present 🙂

Forgive my limited knowledge with php, I guess that is why I use toolset in fact, but your assitance is much appreciated.

I am pretty much done here, but there is one last thing I am having problem with.
Trust I try my best to crawl the forum/ggl and the Toolset documentation, perhaps your expertise will save me precious time again in this busy times...

I have a page listing the logged in user's published posts where they can Edit / Delete after logging in, now in the staging site all is working no problems, however now that I am on the live site, it is not showing the edit button, I checked the access right and all but all right allowed for the user role, and when logged as admin I shouold see it anyway, but no luck there, any idea ?

Thanks
Stephane

#1417917

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Can you try disabling Access on the live site to see whether that affects the visibility of the edit button?

#1417939

Hi Nigel,

Thank you for your reply.
I just disabled the Access plugin but no luck 🙁
s

#1417945

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

What's the connection between the staging site and the live site?

Did you set up everything on the staging site and then when you had everything working automatically duplicate the staging site?

Or did you manually try to set up the same on the live site as you tried on the staging site?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.