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 as of Wordpress 6.0:
global $wp;
$current_url = 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
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. 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?
Someone mentioned that this might be related to the do_parse_request
changes in Wordpress 6.0 outlined here: https://make.wordpress.org/core/2022/04/27/changes-to-do_parse_request-filter-in-wordpress-6-0/ but I'm not so sure about that as this is happening on a Fresh Install of Wordpress with the default theme.
The way of getting the current url should also work in Wordpress 6.
$wp->request
gives you the path structure of the url (like /parent/child ). To pass the whole url as the second argument, you can use the following code:Moreover your problem might be, that you did not wrap your code inside a function, and the data you need is not there yet. You need a hook that fires after the data is available.
But
If you are ok with using another way of getting the current url, I would suggest: