WordPress 中的相对 URL [英] Relative URLs in WordPress

查看:27
本文介绍了WordPress 中的相对 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 URI if you are defining WP_CONTENT_URL.

WordPress 代码说明:

将 WP_CONTENT_URL 设置为此目录的完整 URI(无尾随斜线),例如

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

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

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

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

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

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

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.

推荐答案

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

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:

核心票:

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

  • 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.

任何相对 URL 也会使安装移动时执行转换变得更加困难.在大多数情况下,查找替换将是必要的,而具有讽刺意味的是,出于这些原因,使用绝对 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.

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

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).

[wp-hackers] 线程

[wp-hackers] thread

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

  • 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.

请记住,有两种类型的相对 URL,带和不带前导斜线.两者都有警告,使这不可能正确实施.

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 应该(并且确实)存储绝对 URL.这个不需要对内容进行预处理,没有开销,没有歧义.如果你需要重定位,是数据库中的全局查找替换.

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.

<小时>

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


And, on a personal note, more than once I've found theme and plugins bad coded that simply break when WP_CONTENT_URL is defined.
They don't know this 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.

插件相对网址(链接在edse 的答案),应用函数 wp_make_link_relative动作钩子template_redirect中的一系列过滤器.这是一个非常简单的代码,似乎是一个不错的选择.

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

这篇关于WordPress 中的相对 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆