3

My login link uses wp_login_url( get_permalink() ) so that the user is redirected back to the page they were viewing, and not to the dashboard.

However, this does not include query strings. So if a user was at here.com/?page=4 they would not be redirected back to page 4.

How can I include the query string in my code?

$items .= "<li class='menu-item'> <a href=\"".wp_login_url( get_permalink() )."\">Login</a></li>";

1 Answer 1

6

You can use add_query_arg() to add any query parameters you want to a URI, e.g.,

$redirect_uri = add_query_arg ('page', '4', get_permalink ()) ;
$login_uri = wp_login_url ($redirect_uri) ;

Or, you could simply use $_SERVER['REQUEST_URI'], which will already contain any query string present in the current page's URI, e.g.,

$login_uri = wp_login_url ($_SERVER['REQUEST_URI']) ;
2
  • Thank you! I'm a bit confused by that function, and the explanation on its page. It looks like I need to define every possible value. What if there were a random ID, in which I didn't know what the value was? $redirect_uri = add_query_arg ('random_id', '472938742', get_permalink ()) ; Is there an operator that would allow any value? Or actually, any sanitized value for security?
    – osakagreg
    Commented Feb 23, 2017 at 20:27
  • That's why I'd suggest just passing $_SERVER['REQUEST_URI'] to wp_login_url() (see PHP: Server and execution environment information). For example, if the user were currently viewing here.com/?page=4, that's where they'd go back to after logging it. If they were viewing here.com/?page=4&random_id=472938742, then that's where they'd go back to. Commented Feb 23, 2017 at 21:05

Your Answer

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

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