Okay so what's next? The automatic post generation process is functional during standard wp-admin User creation. If it's not functioning as expected during other User creation processes, that probably indicates you should use a different event hook. If a 3rd-party system is involved, you may need to contact their support team to find a reliable event hook you can use to trigger the automatic post generation process in a way that is compatible with their system.
Since you don't seem to be using a Toolset Form to register Users, the forms redirection API does not apply here. I'm not able to offer much assistance with the actual redirect involved because there is no Toolset Form. I suggest contacting the support team associated with the registration system for more information about redirecting programmatically after automatically generating a new post upon registration.
Let me know what you need from me to proceed here.
That's fair. I am going to use Toolset form to register and see if the user_register hook works. If it does not, I will use cred_save_data hook. Let me try this and get back to you.
Understood, I will stand by for your update.
So, I was able to create a form and I was also able to generate custom post for the user. But, when I ask for redirection after the registration. The user does not stay logged in and the redirection happens to a non logged in page. Am I missing something here in the setup? Rest all looks good.
Images attached.
More context: the url to the page becomes:
hidden link
Registration and login are two separate processes in WordPress. In other words, registration does not automatically log-in the User, so you would need some custom code if you expect automatic login upon registration. Something like this example, since your user_login information is generated automatically: https://toolset.com/forums/topic/auto-login-after-user-registration/page/2/#post-1542205
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 12345 == $form_data['id'] ) { // Edit as required
if ( !empty( $_REQUEST['user_email'] ) && !empty( $_REQUEST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")),
'user_password' => $_REQUEST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
You would replace 12345 with the numeric ID of this User Form.
Chris,
I did what you told me and tried multiple times and there is no login happening after registration but the user is still redirected by the specified page. I am not sure what is the sequence of these triggers and if the login one is even triggering from the form.
The code below:
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 10365 == $form_data['id'] ) { // Edit as required
if ( !empty( $_REQUEST['user_email'] ) && !empty( $_REQUEST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => substr( $_REQUEST['user_email'], 0, strpos( $_REQUEST['user_email'], "@")),
'user_password' => $_REQUEST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
Okay I am investigating this now, and will turn on logs temporarily. I created a backup of the old log file at error_log_bak.txt and cleared out the old content from the current error_log.txt file. I'll give you an update shortly.
I did what you told me and tried multiple times and there is no login happening after registration but the user is still redirected by the specified page. I am not sure what is the sequence of these triggers and if the login one is even triggering from the form.
Let's discuss the code here so you can understand why login does not work now. The automatic login process in your snippet auto-login-after-registration is implemented via the wp_signon function:
https://developer.wordpress.org/reference/functions/wp_signon/
The wp_signon function requires the user login (username) and the user password. In the image you shared user form 2.png, the user login field was configured to be automatically generated, so I shared code to handle that case:
https://toolset.com/forums/topic/auto-login-after-user-registration/page/2/#post-1542205
In the current form, the user login is not automatically generated. It is editable by the User on the front-end, so the code I shared is not applicable. Instead, the wp_signon fuction must supply the dynamic login criteria provided by the User. The original code provided by the client is intended to handle this scenario:
https://toolset.com/forums/topic/auto-login-after-user-registration/#post-1538825
Did you change the login field as a test, or do you intend to make the login field editable in the actual Form? If you plan to make it editable, you can refer to the original code shared by the client in the other ticket. Use this code as a reference to automatically log in the User:
https://toolset.com/forums/topic/auto-login-after-user-registration/#post-1538825
Thanks for the clarification. Just to be clear, this wp_signon code will run only when I am registering through the cred form mentioned in the code. Right? If later on, a registered user signs in then they can do that with their email and password as per my current setup and would not have to provide a user name.
I used the autogenerate feature to test the logic of your system. It seems like abc@email.com results in abc as the author name.
If that is the case, I would like to only use email and password for registration and enable users to add full name, nickname and other information in the edit form.
Let me know your thoughts.
Just to be clear, this wp_signon code will run only when I am registering through the cred form mentioned in the code. Right?
Yes, it's a cred_save_data hook, so this code will be executed when any Toolset Form is submitted. The following line restricts the code further to be executed only when a specific Form is submitted:
if ( 10365 == $form_data['id'] ) { // Edit as required
If later on, a registered user signs in then they can do that with their email and password as per my current setup and would not have to provide a user name.
Right, the default WordPress login system allows you to log in with email address or username. Either is acceptable.
I used the autogenerate feature to test the logic of your system. It seems like abc@email.com results in abc as the author name....If that is the case, I would like to only use email and password for registration and enable users to add full name, nickname and other information in the edit form.
Okay sure, that's what the automatic username generation feature is intended to do. Turn it on to automatically generate unique user login criteria based on their provided email address (though not necessarily identical, since user logins must all be unique). Your users can log in with their email address in the future, they do not need to log in with the automatically generated user name.
Perfect. Let me make the changes and get back to you. Thanks a lot, Chris!
Everything works just fine. Thanks a lot, man.
My issue is resolved now. Thank you!
That is fantastic news. This is a fairly advanced customization, so congratulations!