How can I redirect new users to original url (wantsurl) after updating custom fields?

How can I redirect new users to original url (wantsurl) after updating custom fields?

by S G -
Number of replies: 3

Links to my online courses are posted on various websites. The link look like this : https://example.com/course/view.php?id=31

When new users click on the link, they are brought to my Moodle website login page.

When they sign-up with Google authentication, they are taken to the form where they need to fill up a couple of custom fields.

After they fill in the custom fields and submit, they are taken to preferences.php

Instead of the preferences.php, I need them to go to the original URL, which is stored in "wantsurl"

Right now, I have just edited the preferences.php and used a simple redirect to send users to the dashboard page.

What code can I add to the preferences.php page to redirect users to the original URL that they want to go to? i.e. "wantsurl"

Note: If I remove the custom fields, users are redirected to the course page (wantsurl) after authentication. But the custom fields are essential.

If a user is previously registered on my website, they are properly redirected to "wantsurl" after the Google authentication. It is only when they have to fill in the custom fields that they get redirected to the preferences page.

Average of ratings: -
In reply to S G

Re: How can I redirect new users to original url (wantsurl) after updating custom fields?

by Yew Zhi Yong -
Hi July,

I was also having the same issue. And this was how I solved it.

In /course/user/edit.php, on line 187, change $returnurl = new moodle_url('/user/preferences.php', array('userid' => $user->id)); to $returnurl = $SESSION->wantsurl;.

I'm very new to moodle, and I don't really if there are any implications of the change. But, it works for now.
Average of ratings: Useful (1)
In reply to Yew Zhi Yong

Re: How can I redirect new users to original url (wantsurl) after updating custom fields?

by S G -
It works perfectly smile

Given that the redirection was happening to the dashboard and now it goes to the original URL, there should not be any negative implications.

Thank you so much for posting this.

God bless you & stay safe.
In reply to S G

Re: How can I redirect new users to original url (wantsurl) after updating custom fields?

by Yew Zhi Yong -
Glad to hear that!

I'm sure that there are some use case where the submit button should redirect back to the profile page (eg. when users explicitly go to their profile page to edit). A better solution would be to check IF there is a saved session URL (ie. wantsurl), then only redirect to it; ELSE fallback to preferences.php.

I would leave this to someone much more skilled in PHP than me.