Skip Navigation

[Resolved] Auto login code for multiple forms.

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.

Our next available supporter will start replying to tickets in about 5.97 hours from now. Thank you for your understanding.

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 12 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#1864097

Hi Toolset,

I have the following auto log in code that I will need to work for multiple user forms. It works with one form, but it will not for 2 or more forms even when the form ID is changed. Please help:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
if ($form_data['id']==991)
return 'hidden link';

return $url;
}

#1864239

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

The custom PHP codes you mentioned above is using cred_success_redirect filter hook to redirect user after submit the form, see our document:
https://toolset.com/forums/topic/cred-success-submit-redirection/
It does not do the work "Auto login", and there isn't such kind of built-in feature within Toolset Forms plugin, you might consider other custom codes, for example:
https://toolset.com/forums/topic/auto-login-after-user-registration/page/2/#post-1542205

More help:
https://developer.wordpress.org/reference/functions/wp_signon/
Authenticates and logs a user in with ‘remember’ capability.

#1865043

Hi Luo,

Apologies, I pasted the wrong code. Here is the correct "Auto-Login" code:

<?php

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
* Auto-login new CRED user
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );

function tssupp_cred_autologin( $post_id, $form_data ){

if ( 991 == $form_data['id'] ) { // Edit as required

if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {

// get the user credentials from the $_POST object
$user = array(
'user_login' => $_POST['user_email'],
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );

if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}

}
}
}

This is the same as the one you sent in the link.

How could I make this code work for form ID: 991 and also ID: 992 & 993 & ... etc. I believe I will need an "Array" but I do not know coding very well. Please send over the coding to be able to have this code work for multiple forms.

Thank you,
Andrew

#1865257

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

You can replace this line from:

if ( 991 == $form_data['id'] ) { // Edit as required

With:

$arr = array(991, 992, 993); // add more form IDs here
if ( in_array($form_data['id'], $arr) ) { // Edit as required

And test again.

More help:
hidden link

#1866079

Thank you, looks like it worked.

Lastly, I have a similar question for New Customer Redirect code. How could I make this code work for form ID: 991 and also ID: 992 & 993 & ... etc? Thank you

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
if ($form_data['id']==991)
return 'hidden link';

return $url;
}

New threads created by Luo Yang and linked to this one are listed below:

https://toolset.com/forums/topic/i-have-a-similar-question-for-new-customer-redirect-code/

#1867321

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I assume the original question is resolved, for other new questions, please check the new thread here:
https://toolset.com/forums/topic/i-have-a-similar-question-for-new-customer-redirect-code/