WordPress 中的相对 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17187437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:16:14  来源:igfitidea点击:

Relative URLs in WordPress

wordpress

提问by AidanCurran

I've always found it frustrating in WordPress that images, files, links, etc. are inserted into WordPress with an absolute URL instead of relative URL. A relative url is much more convenient for switching domain names, changing between http and https etc. Today I discovered that if you define WP_CONTENT_URL with a relative url then when you insert files into posts they use the relative url for the src instead of absolute url. Just what I've always wanted! But the official WordPress documentation says that you should use a full URIif you are defining WP_CONTENT_URL.

我总是发现在 WordPress 中使用绝对 URL 而不是相对 URL 将图像、文件、链接等插入 WordPress 令人沮丧。相对 url 更方便切换域名,在 http 和 https 之间切换等。今天我发现,如果你用相对 url 定义 WP_CONTENT_URL,那么当你将文件插入帖子时,它们使用相对 url 作为 src 而不是绝对 url . 正是我一直想要的!但是官方 WordPress 文档说,如果您定义 WP_CONTENT_URL,则应该使用完整的 URI

WordPress codex says:

WordPress 法典 说

Set WP_CONTENT_URL to the full URIof this directory (no trailing slash), e.g.

define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');

将 WP_CONTENT_URL 设置为此目录的完整 URI(没有尾部斜杠),例如

define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');

Everything seems to work fine when I use a relative URL, e.g.

当我使用相对 URL 时,一切似乎都正常,例如

define( 'WP_CONTENT_URL', '/my-content-folder');

But is there some problem with using a relative URI? I'm just thinking that there must be a reason for WordPress stating that it should be defined with a full URI.

但是使用相对 URI 有什么问题吗?我只是在想 WordPress 必须有一个理由声明它应该用完整的 URI 来定义。

采纳答案by brasofilo

I think this is the kind of question only a core developer could/should answer. I've researched and found the core ticket #17048: URLs delivered to the browser should be root-relative. Where we can find the reasons explained by Andrew Nacin, lead core developer. He also links to this [wp-hackers] thread. On both those links, these are the key quotes on why WP doesn't use relative URLs:

我认为这是只有核心开发人员才能/应该回答的问题。我研究并发现了核心票 #17048:传递给浏览器的 URLs should be root-relative。我们可以在哪里找到核心开发人员 Andrew Nacin 解释的原因。他还链接到这个 [wp-hackers] 线程。在这两个链接上,以下是 WP 为何不使用相对 URL 的关键引述:

Core ticket:

核心票:

  • Root-relative URLs aren't really proper. /path/might not be WordPress, it might be outside of the install. So really it's not much different than an absolute URL.

  • Any relative URLs also make it significantly more difficult to perform transformations when the install is moved. The find-replace is going to be necessary in most situations, and having an absolute URL is ironically more portable for those reasons.

  • absolute URLs are needed in numerous other places. Needing to add these in conditionally will add to processing, as well as introduce potential bugs (and incompatibilities with plugins).

  • 相对于根目录的 URL 并不合适。/path/可能不是 WordPress,它可能在安装之外。所以实际上它与绝对 URL 没有太大区别。

  • 移动安装时,任何相对 URL 也会使执行转换变得更加困难。find-replace 在大多数情况下都是必要的,并且具有讽刺意味的是,由于这些原因,拥有绝对 URL 更具可移植性。

  • 许多其他地方都需要绝对 URL。需要有条件地添加这些将增加处理,并引入潜在的错误(以及与插件的不兼容)。

[wp-hackers] thread

[wp-hackers] 线程

  • Relative to what, I'm not sure, as WordPress is often in a subdirectory, which means we'll always need to process the content to then add in the rest of the path. This introduces overhead.

  • Keep in mind that there are two types of relative URLs, with and without the leading slash. Both have caveats that make this impossible to properly implement.

  • WordPress should (and does) store absolute URLs. This requires no pre-processing of content, no overhead, no ambiguity. If you need to relocate, it is a global find-replace in the database.

  • 相对于什么,我不确定,因为 WordPress 通常位于子目录中,这意味着我们总是需要处理内容,然后添加到路径的其余部分。这引入了开销。

  • 请记住,有两种类型的相对 URL,带前导斜杠和不带斜杠。两者都有使这无法正确实施的警告。

  • WordPress 应该(并且确实)存储绝对 URL。这不需要对内容进行预处理、没有开销、没有歧义。如果需要重定位,则是数据库中的全局查找替换。



And, on a personal note, more than once I've found theme and plugins bad coded that simply break when WP_CONTENT_URLis defined.
They don't knowthis can be set and assume that this is true: WP.URL/wp-content/WhatEver, and it's not always the case. And something will break along the way.

而且,就个人而言,我不止一次发现主题和插件编码错误,在WP_CONTENT_URL定义时就会中断。
他们不知道这可以设置并假设这是真的:WP.URL/wp-content/WhatEver,但情况并非总是如此。在此过程中有些东西会破裂。



The plugin Relative URLs(linked in edse's Answer), applies the function wp_make_link_relativein a series of filters in the action hook template_redirect. It's quite a simple code and seems a nice option.

插件相对 URL(链接在edse的 Answer 中),在 action hookwp_make_link_relative中的一系列过滤器中template_redirect应用该函数。这是一个非常简单的代码,似乎是一个不错的选择。

回答by davidcondrey

<?php wp_make_link_relative( $link ) ?>


Convert full URL paths to relative paths.

Removes the http or https protocols and the domain. Keeps the path '/' at the beginning, so it isn't a true relative link, but from the web root base.

Reference: Wordpress Codex

将完整的 URL 路径转换为相对路径。

删除 http 或 https 协议和域。将路径 '/' 保留在开头,因此它不是真正的相对链接,而是来自 Web 根基。

参考:Wordpress 法典

回答by danielsalare

I agree with Rup. I guess the main reason is to avoid confusion on relative paths. I think wordpress can work from scratch with relative paths but the problem might come when using multiple plugins, how the theme is configured etc.

我同意鲁普。我想主要原因是为了避免在相对路径上混淆。我认为 wordpress 可以使用相对路径从头开始工作,但是在使用多个插件、主题如何配置等时可能会出现问题。

I've once used this plugin for relative paths, when working on testing servers:

在测试服务器时,我曾经将此插件用于相对路径:

Root Relative URLs
Converts all URLs to root-relative URLs for hosting the same site on multiple IPs, easier production migration and better mobile device testing.

根相对 URL将所有URL
转换为根相对 URL,以便在多个 IP 上托管同一站点、更轻松的生产迁移和更好的移动设备测试。

回答by ikebastuz

I solved it in my site making this in functions.php

我在我的网站上解决了这个问题,在functions.php

add_action("template_redirect", "start_buffer");
add_action("shutdown", "end_buffer", 999);

function filter_buffer($buffer) {
    $buffer = replace_insecure_links($buffer);
    return $buffer;
}
function start_buffer(){
    ob_start("filter_buffer");
}

function end_buffer(){
    if (ob_get_length()) ob_end_flush();
}

function replace_insecure_links($str) {

   $str = str_replace ( array("http://www.yoursite.com/", "https://www.yoursite.com/") , array("/", "/"), $str);

   return apply_filters("rsssl_fixer_output", $str);

}

I took part of one plugin, cut it into pieces and make this. It replaced ALL links in my site (menus, css, scripts etc.) and everything was working.

我拿了一个插件的一部分,把它切成碎片并制作了这个。它替换了我网站中的所有链接(菜单、CSS、脚本等),并且一切正常。

回答by Botond Vajna

should use get_home_url(), then your links are absolute, but it does not affect if you change the site url

应该使用get_home_url(),那么你的链接是绝对的,但是不影响你是否改变了站点的url

回答by Shiva

What i think you do is while you change domain names, the sql dump file that you have you can replace all instances of old domain name with new one. This is only option available as there are no plugins that will help you do this.

我认为您所做的是在更改域名时,您拥有的 sql 转储文件可以用新域名替换旧域名的所有实例。这是唯一可用的选项,因为没有可以帮助您执行此操作的插件。

This is quickest way ..

这是最快的方法..

回答by Ger

There is an easy way

有一个简单的方法

Instead of /pagename/use index.php/pagename/or if you don't use permalinks do the following :

取而代之的/pagename/使用index.php/pagename/,或者如果你不使用固定链接做到以下几点:

Post

邮政

index.php?p=123

Page

index.php?page_id=42

Category

类别

index.php?cat=7

More information here : http://codex.wordpress.org/Linking_Posts_Pages_and_Categories

更多信息在这里:http: //codex.wordpress.org/Linking_Posts_Pages_and_Categories