Trying to obtain the page url with the following code prior to Wordpress 6.0 works, but after update to Wordpress 6.0, $wp->request is coming through as an empty string on all pages. I have Post name set in permalinks under Common Settings if that matters.

The code below no longer works for getting the current url in the browser:

add_query_arg($wp->query_vars, $wp->request);

If there a new way to obtain the current url in the browser with Wordpress 6.0? I would need to obtain it as early as possible. The code above worked as is directly within the functions.php file without any hooks. Is there something similar to this in Wordpress 6.0?

EDIT: Tested with a Fresh install of Wordpress 6.0

This is also happening after installing a Fresh copy of Wordpress 6.0 in the twentytwentytwo theme functions.php file (placed the following at the top of the file):

global $wp;

$current_url = add_query_arg($wp->query_vars, $wp->request);

error_log(var_export($current_url, true));
error_log(var_export($wp->request, true));

Look at wp-content/debug.log and see that both are empty strings no matter what url you go to on the site, or what your permalink settings are set to. Obviously, you will need to enable the debug log in wp-config.php first.

What is the correct way in Wordpress to obtain the current page url?

  • Yes, I'm positive that the code worked directly in the functions.php file at the very top and outputted the correct url, for example, in header.php and in footer.php on pages it could be used easily, and was in the functions.php file without any hooks. It was also possible to get the page url from an ajax call easily as well. May 25, 2022 at 22:00
  • And there might be a better way to do what you're trying to do, so why do you need to get the current URL and why must it be directly in the functions file?
    – Sally CJ
    May 25, 2022 at 22:00
  • Because I need to obtain it from ajax calls, where many wordpress actions don't get called. May 25, 2022 at 22:01
  • But I also need to check the url for when a form gets submitted because I'm using the url as a slug to get data from wp_options table in some instances where specific api keys are attached to urls on the site. May 25, 2022 at 22:06
  • 1
    e.g. what you've said implies the $wp->request code is running in a classic admin-ajax.php handler, but AJAX handlers don't have favicons and images, are you actually running this on the frontend then sending it to an AJAX handler? Please provide more context for what you're doing and more code for what surrounds this, as well as how and where you are processing it. There's a very strong possibility that you've built something in a way that has a much simpler alternative, or that you're hiding key information that you don't think is relevant to keep it super abstract/generic
    – Tom J Nowell
    May 26, 2022 at 0:23

1 Answer 1


What is the correct way in Wordpress to obtain the current page url?

You don't, WP has never provided a way to do this, the closest is get_permalink which provides the canonical URL of the current post.

What you've been using is unofficial, and never gave you the actual URL as it only appended the whitelisted query variables that WP_Query accepts.

Instead, WordPress is just a CMS built in PHP, and PHP has a solution. You can use $_SERVER['REQUEST_URI'] to get the full URL, instead, then parse out only the parts you want.


$url =  "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$path = parse_url( $url, PHP_URL_PATH );
add_query_arg( $wp->query_vars, $path );


Additionally, you might have do_parse_request filters that return false or an equivalent value, or most likely they return nothing at all ( aka null ), causing WP to bypass this. The cause of this is a change in WordPress 6.0 that was announced in April:


However this is unlikely to be the cause of your issue.

  • There is no filter do_parse_request in the theme at all, so are you saying I would need to add it in order to obtain a non-empty string from $wp->request? May 25, 2022 at 22:42
  • There is also no do_parse_request filters added to any of the plugins installed. So not sure why it is returning empty string. I'm going to test now with a completely new install of Wordpress to be sure. Thanks for finding that info. May 25, 2022 at 22:48
  • I'm not sure if that is related to the problem honestly, but we'll find out on a completely fresh install of Wordpress 6.0 with No plugins. May 25, 2022 at 22:52
  • 1
    also funfact: add_query_arg($wp->query_vars,''); works in WP 5.8, $wp->request doesn't need to contain anything, add_query_arg will actually attempt to use $_SERVER['REQUEST_URI'] if no URL is provided which may have been what was letting your original code work all along developer.wordpress.org/reference/functions/add_query_arg. Consider trying add_query_arg($wp->query_vars )
    – Tom J Nowell
    May 26, 2022 at 0:45
  • 2
    Actually @SolomonClosson, add_query_arg( $wp->query_vars ) in your case, is equivalent to add_query_arg( array() ) and add_query_arg( '' ).. if I was correct that you're running the code on the front-end or the page where the AJAX request is being made from. Because if so, then the issue in question happened because $wp->query_vars was an empty array because WordPress hadn't yet parsed the request. In previous WordPress versions, that property defaults to a NULL which basically why add_query_arg() gave you a correct URL - or used the REQUEST_URI value.
    – Sally CJ
    May 26, 2022 at 9:59

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.