简体   繁体   English

如何在 Wordpress 6.0 中获取当前页面 URL?

[英]How to get current page URL 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.在 Wordpress 6.0 之前尝试使用以下代码获取页面 url,但在更新到 Wordpress 6.0 之后, $wp->request在所有页面上都作为空字符串出现。 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:自 Wordpress 6.0 起,以下代码不再适用于在浏览器中获取当前 url:

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?如果有一种新的方法可以使用 Wordpress 6.0 在浏览器中获取当前 url? 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.上面的代码直接在 functions.php 文件中工作,没有任何钩子。 Is there something similar to this in Wordpress 6.0? Wordpress 6.0 中有类似的东西吗?

EDIT: Tested with a Fresh install of Wordpress 6.0编辑:使用全新安装的 Wordpress 6.0 进行测试

In the twentytwentytwo theme functions.php file (placed the following at the top of the file):在二二二二主题 functions.php 文件中(将以下内容放在文件顶部):

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.查看wp-content/debug.log并查看无论您转到哪个 url,两者都是空字符串。 Obviously, you will need to enable the debug log in wp-config.php first.显然,您需要先在 wp-config.php 中启用调试日志。

What is the correct way in Wordpress to obtain the current page url? Wordpress中获取当前页面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.有人提到这可能与此处概述的 Wordpress 6.0 中的do_parse_request更改有关: https ://make.wordpress.org/core/2022/04/27/changes-to-do_parse_request-filter-in-wordpress-6-0 /但我不太确定,因为这是在使用默认主题的全新安装的 Wordpress 上发生的。

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 ).获取当前 url 的方式也应该适用于 Wordpress 6。 $wp->request为您提供 url 的路径结构(如 /parent/child )。 To pass the whole url as the second argument, you can use the following code:要将整个 url 作为第二个参数传递,您可以使用以下代码:

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

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:如果您可以使用另一种获取当前网址的方式,我建议:

function getCurrentUrl() {
    $protocol = is_ssl() ? 'https://' : 'http://';
    return ($protocol) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

$currentUrl = getCurrentUrl();
echo $currentUrl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM