0

I want to redirect subscribers to a special page after login to my WordPress Site ("http://www.my-cool-page.com/coolpeople") where "coolpeople" is a regular WordPress page. This is my code to redirect only the "subscribers":

$internStart = 'evenmorecoolpeople';
function loginRedirect( $redirect_to, $request_redirect_to, $user ) {
    if ( is_a( $user, 'WP_User' ) && $user->has_cap( 'edit_posts' ) === false ) {
        echo 'return URL: ' . home_url('/') . $internStart; die ();
        return home_url();
    }
    return $redirect_to;
}
add_filter( 'login_redirect', 'loginRedirect', 10, 3 );

Now I want to replace "coolpeople" with a variable like:

$internStart = 'evenmorecoolpeople';

but ...

return home_url($internStart);

or ...

return home_url('/') . $internStart;

Does not work. Any Idea?

17
  • 1
    What do you get if, before return home_url, you put in the following code: echo 'return URL: ' . home_url('/') . $internStart; die (); ?
    – iguanarama
    Jan 6, 2017 at 15:36
  • 1
    Perhaps show us all the whole loginRedirect function where you're trying to use the $internStart variable.
    – iguanarama
    Jan 6, 2017 at 15:45
  • 1
    If you've put my echo line inside the brackets, before return home_url, but the redirect is still happening, then the if conditions are not being met. The line I gave you should output the URL that is being used for the redirect, and then should not redirect.
    – iguanarama
    Jan 6, 2017 at 15:50
  • 2
    It is a variable scope issue, test by putting a line with global $internStart; at the begin of your function. Jan 6, 2017 at 15:59
  • 2
    @something, you are right, and even better is to not use global variables at all ;) Jan 6, 2017 at 16:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.