I am a bit puzzled as to the usage of the login_redirect filter.

I have two situations when I want to use this:

  1. User (or admin) is logging in to access members area
  2. Admin is logging in to access backend

The behaviour I would like to have is this:

  1. User goes to the "member only" area of the site (/member-area/somepage/). He gets a link to the login page (/wp-login.php?redirect_to=member-area/somepage/), clicks, logs in and gets redirected to the specific page in the member area.

  2. Admin goes to an admin page (e.g. /wp-admin/options-general.php?page=myplugin_options ). He gets automatically redirected to the login page (wp-login.php?redirect_to=/wp-admin/options-general.php?page=myplugin_options), logs in and gets redirected to the admin page.

The link and/or redirection to wp-login.php works without problems, however, I have an issue with the redirection part. Right now point 1. works, and users get redirected to the page they were trying to access. However, if an admin tries to access a backend page he just gets redirected to the login page again and again...

This is the code I'm using in functions.php

// Redirect to referrer after login
function redirect_on_login()
    if (isset($_GET["redirect_to"]))
        $referrer = $_GET["redirect_to"];
        $referrer = wp_get_referer();

    $homepage = get_option('siteurl');

    if (empty($referrer))
        return $homepage;
        return $referrer;

add_filter('login_redirect', 'redirect_on_login', 10);

Also, as a side question, should I sanitize $_GET["redirect_to"] ?

  • 1
    I feel like disabling your filter would actually fix the problem. ?redirect_to is already handled by WordPress - you just need to pass the ?redirect_to=/members-area from the front end and the rest should be done for you. – Eric Holmes Aug 7 '15 at 16:43
  • @EricHolmes unfortunately that does not seem to be the case... if I disable the filter redirect_to does not seem to do anything, users get redirected to the homepage... I'll try and check if any other plugin is interfering with this. – nico Aug 7 '15 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.