Why is $wp->request empty in Wordpress 6.0?

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?

Topic php urls Wordpress

Category Web


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.

E.g.

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

https://www.php.net/manual/en/function.parse-url.php


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:

https://make.wordpress.org/core/2022/04/27/changes-to-do_parse_request-filter-in-wordpress-6-0/

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.