Skip Navigation

[Resolved] function to auto-login after user registration won't work on multisite

This support ticket is created 8 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 10 replies, has 2 voices.

Last updated by Shane 7 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#390642

Jay

I realize auto login after user registration is not native behavior to CRED user forms (yet) , however there is an excellent post (along with a video) by Simon Toulson on CRED User forms that does just that with a function.

hidden link

He has implemented it successfully with the following:

function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);

// If we logged in from our CRED registration form (id = 389), then redirect to our owner area,
// otherwise, send user to the home page

if isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 389 ) {
$url='/owner-area/';
} else {
$url= home_url();
}
wp_redirect( $url );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );

I have entered nearly the identical snippet into my functions file - just replacing my CRED User Form ID from what he had. And my user account page slug with what he had as well:

function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);

// If we logged in from our CRED registration form (id = 542), then redirect
// to our post author account page, otherwise, send user to the home page

if isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 ) {
$url='/account/';
} else {
$url= home_url();
}
wp_redirect( $url );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );

But I receive the following error:

Parse error: syntax error, unexpected 'isset' (T_ISSET), expecting '(' in ......

I'm assuming it's because I'm on a multisite install? Or is there a problem with that function?

Any suggestions how to get this to work on a multisite setup? It sure would be a great feature to add to CRED User Forms.

Any insight would be greatly appreciated!
Cheers

#390653

Jay

Alternatively, I came across a post from Minesh which accomplished the same thing using "cred_save_data" hook and wp_signon() function, however I'm not sure how to completely put it together?

https://toolset.com/forums/topic/log-in-user-after-completing-registration-form/#post-345367

If the method in my first post isn't possible, is Minesh's idea a viable solution?

Thanks again!

#390694

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jason,

I took a look at your code and its seems you have a syntax error in the following line.

if isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 389 ) 

and

if isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 )

They should be changed to

if ( isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 389 ) )

and

if (isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 ))

Please let me know if this helps.
Thanks,
Shane

#390711

Jay

Thanks for the response Shane. The first code snippet was from the site I found the article on, the second was my own version.

I tried to make the changes you suggested and ended up with the following:

function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
if (isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 )) {
$url='/account/';
} else {
$url= home_url();
}
wp_redirect( $url );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );

However, now I am getting the following error:

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

Any ideas? Thanks again!

#390713

Jay

After a quick google search, I found someone with a similar issue and updated that single line accordingly.

You provided me this change:

if (isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 ))

And as I mentioned, I got a fatal error.

I changed it to this:

if (isset( $_POST['_cred_cred_prefix_form_id'] ) && ( $_POST['_cred_cred_prefix_form_id'] === 542 ))

and I no longer get the fatal error and registration happens fine, however, it redirects me to my homepage rather than my /account/ directory.

Perhaps my change wasn't correct despite it not throwing the error? Or perhaps it has something to do with multisite and using subdirectories?

Any additional ideas are welcome! Thanks again Shane!

#390726

Jay

So sorry for the three posts in a row, but I mispoke in the last post.

I should have said, the changes I made to your edit allows me to now complete registration fine... AND it logs me in automatically like I desired.

It just doesn't redirect me to the user account page like it should.

Thanks again. 🙂

#390735

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jason,

I suspect the issue is with your if statement condition not being met.
if (isset( $_POST['_cred_cred_prefix_form_id'] && $_POST['_cred_cred_prefix_form_id'] === 542 )) {

Could you try doing this?

if (isset( $user_id)) {

This should check if the user id has been set and then allow you to redirect to the account page.

Please let me know if this helps.
Thanks,
Shane

#390749

Jay

Hi Shane, Thanks again for the assistance!

Ok, so I now have the following:

function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
if (isset( $user_id)) {
$url='/account/';
} else {
$url= home_url();
}
wp_redirect( $url );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );

The above function worked. I registered an account and it auto logged me in.

However, as I am running multisite, it redirected me to:

mysite.com/account/

I need it to redirect me to:

mysite.com/site-subdir/account/

What change do I need to make to account for the subdirectory the site I'm testing is in on a multisite setup?

Thanks again so much!!!

#391044

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jason,

What you can do is set the url variable to .

$url = "site-subdir/account/"

Please let me know if this helps.
Thanks,
Shane

#393876

Jay

Hi Shane,

I'm beginning to add more sites to my multisite install. If I have multiple sites, how should I adjust this bit:

$url = "site-subdir/account/"

Each of my sites is a County in the U.S. Is there a piece of code I can put in it's place that will detect what site it's rendering?

Thanks again!

#394261

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jason,

What you can do is try this function.
get_current_site();

Add it like this.

$current_site = get_current_site();

$url = $current_site->path;

Please let me know if this helps. For more information on this function you can have a look at the link below.

https://codex.wordpress.org/WPMU_Functions/get_current_site

Thanks,
Shane

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