0

I am using a custom login page witha script in the functions.php that redirect to a custom "error login" page (adding parameters to the login URL), so it can display the errors in the same page instead of the native login page.

But in some cases, this login page contains some parameters already, when someone is redirected there after trying to access a private page.

For example :

  • Regular case : They try to access : website.com/login. If there is an error when trying to login, my script redirect to website.com/login?login=empty, and they can try again.

  • Problematic case : They try to access website.com/protected-page/private, they are automatically redirected to website.com/login?members=%2Fprotected-page%2Fprivate%2. If there is an error when trying to login, my script actualy redirect to website.com/login?login=empty (same as regular case), and what I'm trying to achieve, is to redirect to website.com/login?members=%2Fprotected-page%2Fprivate%2&login=empty, when an error is made.

Keeping this "?members" parameter in the URL allow the form to redirect to the previous page they were trying to access before, after a successful login. Otherwise with the regular case, they are redirected to the general dashboard.

The script that actually manage the redirect when error is this one :

add_action( 'authenticate', 'check_username_password', 1, 3); function check_username_password( $login, $username, $password ) {

$referrer = $_SERVER['HTTP_REFERER'];

if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) { 
if( $username == "" || $password == "" ){
    wp_redirect( get_permalink( 20 ) . "?login=empty" );
    exit;
}
}

So the important part here, that will create the error URL is :

wp_redirect( get_permalink( ID ) . "?login=empty" );

I can get my other UTL parameter with "$_GET["members"]", if I do an "echo $_GET["wlfrom"];" it displays successfully the parameter in the page. But even if it looks so simple, I don't manage to add this parameter to the URL. What I'v tried so far :

#1. wp_redirect( get_permalink( ID ) . "?members=" . $_GET["members"] . "?login=empty" );
#2. wp_redirect( get_permalink( ID ) . "?members=" . echo $_GET["members"] . "?login=empty" );
#3. $param = array('members');
wp_redirect( get_permalink( ID ) . "?members=" . $param . "?login=empty" );
//This one returns website.com/?members=array/?login=empty
#4.$param = $_SERVER["PHP_SELF"];
wp_redirect( $param . "?login=empty" );
// This one I cannot use because it displays the native login URL, not my permalink, that's why I need to use "get_permalink" for the first part of the URL

Lately I tried this solution that sounded like the best I had so far :

New hidden field in the login form :

<input type="hidden" name="members" value="<?php echo @$_REQUEST['members']; ?>">

replacing : wp_redirect( get_permalink( ID ) . "?login=empty" ); by

$queryarg='?login=empty';
if(isset($_REQUEST['members']) && $_REQUEST['members']!=''){
    $queryarg .='&members='$_REQUEST['members'];
}
wp_redirect( get_permalink( ID ) . $queryarg );

But this solution has returned :

Parse error: syntax error, unexpected '$_REQUEST' (T_VARIABLE) on line 113.

The line involved beeing $queryarg .='&members='$_REQUEST['members']

So any solution to make that happen ? What should I fill up "wp_redirect" with in order to use the parameter from the current URL ?

Thank you !

1

I suggest researching add_query_arg. You can use it like this:

$params_array = array();

foreach($_GET as $key => $param){
    $params_array[] = $key . '=' . urlencode($param);
}

$url = get_permalink(ID) . '?' . implode('&', $params_array);

wp_redirect( esc_url_raw( add_query_arg( 'login', 'empty', $url ) ) );

or you can add multiple params like this:

$params_array = array();

foreach($_GET as $key => $param){
    $params_array[] = $key . '=' . urlencode($param);
}

$url = get_permalink(ID) . '?' . implode('&', $params_array);

$args = array(
    'login' => 'empty'
    'param2' => 1
);

wp_redirect( esc_url_raw( add_query_arg( $args, $url ) ) );

There is also an optional $url param, without it $_SERVER['REQUEST_URI']( or the current url) is used as the subject url, and since it's the current url (with params) that you need it will just append the new param(s) right to the end of your url.

Note the esc_url_raw, the add_query_arg function doesn't escape by default, calling for a "late escape", technically you could use esc_url also, but it's not needed since you aren't displaying the link.

  • Hi stims, thank you for your answer. I tried both of your solutions, the first one returned "wp-login.php?login=empty". So it ignored my "members" parameter, and it returned as it's asked the "raw url", which is the wp-login.php when there is a query. I want them to stay on my customized page, which is why I can only use "get permalink" as a first parameter. $url didn't work for me either, it is just beeing ignored. Is there any way to just use the "members" parameter to add it to get permalink and create this new URL ? – Jimmy Gibs Oct 18 '16 at 11:22
  • @JimmyGibs I've updated my response, if its still not working you may want to make your question a little be clearer and more concise. As for the raw url comment, its actually not doing what you think it is, its just a escapes certain characters, it doesn't remove url params. – stims Oct 18 '16 at 13:39
  • Also, wp_redirect requires an exit(); after it to work correctly. – stims Oct 18 '16 at 13:42
  • Hi, yes I do have the exit in the end of the code, I didn't removed it when trying it. – Jimmy Gibs Oct 18 '16 at 17:54
  • Try it with the updated code, and let me know the outcome, thanks! – stims Oct 18 '16 at 19:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.