Jimmy Gibs
Jimmy Gibs

Reputation: 31

Use a $GET parameter from a URL, to create a new redirect URL (Wordpress)

I am using a custom login page in Wordpress, and I installed a 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 :

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

And various other solutions too. I'm a newbie, so I can't find of other solutions to create this new URL based on the previous one, I'm not sure either if using GET is the way to go also.

What should I fill up "wp_redirect" with in order to use the parameter from the current URL ?

Thank you !

Upvotes: 0

Views: 3843

Answers (1)

Anand Kahar
Anand Kahar

Reputation: 11

put a hidden field in your wordpress custom login form

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

and change these

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

with

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

?>

Upvotes: 0

Related Questions