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.