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;
}
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.
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
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
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;
}
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/