General developer forum

Redirect for logged in users

Picture of Carmen Griffiths
Redirect for logged in users

Hi everyone, I was searching for close to a whole day to figure out how to redirect users to My Profile page instead of the normal dashboard, site and my pages.. I tried and tested for hours.. So just thought i would share this. Initially i thought i had to make changes to the login/index.php BUT FOUND the way around... thought I would share in case someone else could use it as the redirect login/logout plugin is no compatible with 3.5+

Take a look in moodle 3.5.2 at the login/lib.php, round lines 307 for the function:

function core_login_get_return_url() 

See my changes to $urltogo -- >  /user/profile.php?id=

SNIPPET in Context:

function core_login_get_return_url() {

    global $CFG, $SESSION, $USER;

    // Prepare redirection.

    if (user_not_fully_set_up($USER, true)) {

        $urltogo = $CFG->wwwroot.'/user/edit.php';

        // We don't delete $SESSION->wantsurl yet, so we get there later.

    } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0

            or strpos($SESSION->wantsurl, str_replace('http://', 'https://', $CFG->wwwroot)) === 0)) {

        $urltogo = $SESSION->wantsurl;    // Because it's an address in this site.


    } else {

        // No wantsurl stored or external - go to homepage.

        $urltogo = $CFG->wwwroot.'/';



    // If the url to go to is the same as the site page, check for default homepage.

    if ($urltogo == ($CFG->wwwroot . '/')) {

        $homepage = get_home_page();

        // Go to my-moodle page instead of site homepage if defaulthomepage set to homepage_my.

        if ($homepage == HOMEPAGE_MY && !is_siteadmin() && !isguestuser()) {

            if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {

                //  changed to redirect to profile page

                $urltogo = $CFG->wwwroot.'/user/profile.php?id=';




    return $urltogo;


Average of ratings: -