Home_url()和site_url()有什么不同

时间:2011-06-17 作者:Praveen

我的理解是site_url() 返回WordPress核心文件所在的位置。

如果我的博客位于http://example.com/blog 然后site_url() 退货http://example.com/blog

但那怎么办呢home_url() 相异对我来说,home_url() 返回相同的内容:http://example.com/blog

如果这是正确的,那么我可以让WordPress返回吗http://example.com/ ?

6 个回复
最合适的回答,由SO网友:Volomike 整理而成

您同时提出了两个问题:

两者的区别是什么home_url()site_url()?

问题#1

一般>wp admin的设置,home_url() 引用标记为“站点地址(URL)”的字段<很困惑吧site_url(), 但是you\'d be wrong. 运行您自己的测试,您将看到。(您可以临时删除echo H1 具有的字段site_url()home_url() 主题功能顶部的值。php。)

同时site_url() 参考常规>设置中标记为“WordPress地址(URL)”的字段。

因此,如果您想引用物理路径的位置,例如在URL上调用插件的文件夹路径来加载图像,或者调用主题的文件夹路径来加载图像,那么您应该实际使用其他函数来加载这些路径-请看plugins_url()get_template_directory_uri().

这个site_url() 将始终是您可以通过定位钉到达站点的位置/wp-admin 最后home_url() 不可能是这个位置。

这个home_url() 您可以通过设置General>Settings“Site Address(URL)”字段来设置主页。

问题#2

那么,如果我在http://example.com/blog, 和example.com 只是一些静态站点,我喜欢一个公文包主题,那么这将是一个符合您的问题的场景。在这种情况下,我将使用以下代码片段:

<?php
function getDomain() {
    $sURL    = site_url(); // WordPress function
    $asParts = parse_url( $sURL ); // PHP function

    if ( ! $asParts )
      wp_die( \'ERROR: Path corrupt for parsing.\' ); // replace this with a better error result

    $sScheme = $asParts[\'scheme\'];
    $nPort   = $asParts[\'port\'];
    $sHost   = $asParts[\'host\'];
    $nPort   = 80 == $nPort ? \'\' : $nPort;
    $nPort   = \'https\' == $sScheme AND 443 == $nPort ? \'\' : $nPort;
    $sPort   = ! empty( $sPort ) ? ":$nPort" : \'\';
    $sReturn = $sScheme . \'://\' . $sHost . $sPort;

    return $sReturn;
}

SO网友:SherylHohman

TLDR:

In a non-standard installation, you can place your WordPress files in a subdirectory of your website root.
...and still allow your visitors to visitors to access your WordPress "website" from your site\'s Domain (root) URL, without appending the subdirectory name:
(ie: www.example.com vs www.example.com/wordpress):

WP function  | wp_options. | WP constant  | what it represents       | WP Settings Label | Example     
-------------------------------------------------------------------------------------------------------------------------------------
`site_url()` | `siteurl`   | `WP_SITEURL` | WordPress files location | WordPress Address | https://www.example.com/wordpress
`home_url()` | `home`      | `WP_HOME`    | browser address bar      | Site Address      | https://www.example.com 

Where the value for a WP constant takes precedence over an wp_options/ WP Settings value.

Different Configurations for WordPress

In the most standard WordPress installations, home_url and site_url will have the same value.
Regardless, they represent two different things.

In a non-standard installation, they may have different values.

NOTE: I am leaving off the protocol in my answer for easier readability.
In this post, PREPEND EVERY URL with: https://, http:// OR //
(unless I included it already).

(// is the relative protocol and will work for either/both http:// or https://)

Standard Installations (including "One-Click" Installs)

home_url: is the home page of your (wordpress) website, as indicated in the user\'s address bar.
site_url: is the directory where your wordpress files are located.

WordPress\'s 5-minute install installs wordpress files these two values will be the same - wordpress files will be installed in the same folder that you want people to use to address your website, or the wordpress (blog) portion of your server\'s website.

Example 1:
user accesses your blog at: www.example.com,
wordpress files installed at: www.example.com, or the root folder of your server\'s website.

home_url === site_url === "www.example.com"

Example 2:
user accesses your blog at: www.example.com/blog,
wordpress files installed at: www.example.com/blog, or in the blog folder inside the root of your website.

home_url === site_url === "www.example.com/blog"

In this case www.example.com is the main website, and www.example.com/blog is the root of your blog.
Here your blog is separated from, and works as subset of, your main website.
In this case, your main website is not controlled, defined, or styled by WordPress.
Just your blog is. All urls in your blog will be proceeded by www.example.com/blog

Note: In documentation, "Wordpress site/website" (as opposed to simply "site/website") refers to the directory where your WordPress files are installed. In this case, it is www.example.com/blog - everything within the blog folder. The "WordPress website", in this scenario, is not the same as your domain, your root, or your main website. It is a subset of your overall website. Kind of like a website inside a website. I mention this as the terminology can seem unclear or confusing, given this particular setup.

Alternate WordPress Installation Configuration

Giving WordPress Its Own Directory, the section Method II (With URL change).

For example, many people don\'t want to clog up the root folder of their website with all the wordpress files.
They want to install wordpress in a subdirectory, *but have the "blog" or "WordPress website" accessed as if the files were installed in the root of the server\'s root for the website.

This is particularly be true when WordPress is used to build and run an entire website that does not even have a "blog".

Example 3:
user accesses your "blog" at: www.example.com,
wordpress files installed at: www.example.com/wordpress, or the root folder of your server\'s website.

home_url === "www.example.com"
site_url === "www.example.com/wordpress"

(Note: this configuration will not work "out of the box" just by changing the values of these variables. It requires additional configuration changes to work properly)
See Giving WordPress Its Own Directory, the section titled Method II (With URL change) for how to do this.

In this case home_url and site_url should hold different values.

In this setup, you want your website to function exactly as if WordPress files were installed in the server\'s root directory for your website...
BUT, for organizational purposes on the server,
you actually have your WordPress files in a folder called wordpress in the server\'s root directory for your website.

So, user will type in www.example.com to get your WordPress home page, instead of www.example.com/wordpress

wordpress function <--> database variable <--> Wordpress Constant

This section assumes Example 3 configuration above.
address bar url: www.example.com
wordpress files: /wordpress directory

(The other cases are trivial: All variables/functions hold/return the same value.)

How to set the values for site_url and home_url

First, let me note that siteurl and home store values returned by the functions above

1) Normally you set these values on the WordPress backend/dashboard/admin panel:
Settings -> General ->
siteurl WordPress Address: https://www.example.com/wordpress
home Site Address: https://www.example.com

(do not include trailing slashes here - that would be configured elsewhere)

2) Alternatively, you set these values in your WordPress database:
wp_options table ->

`options_name` | `options_value`
----------------------------------------------------
`siteurl`      | `https://www.example.com/wordpress`  
`home`         | `https://www.example.com`  

(do not include trailing slashes here - that would be configured elsewhere)

3) Edit your wp-config.php
Define these specific constants to hold your values
Define WP_HOME and WP_SITEURL settings by inserting these lines toward the top of your wp-config.php file:

define(\'WP_SITEURL\',\'http://example.com/wordpress\');  // wordpress core files
define(\'WP_HOME\',\'http://example.com\');               // address bar url

// ** MySQL settings - You can get this info from your web host ** //
...    

(do not include trailing slashes here - that would be configured elsewhere)

Reference: WP_SITEURL and WP_HOME

NOTE: This is confusing
(I really wish WordPress had Labeled the Settings similar to their php names,
such as Wordpress Site Address and Home Page Address or something more explicit like location of WordPress Site core files and browser url to access WordPress home page)

`WP_SITEURL` <--> `site_url()` <--> `siteurl` <--> Wordpress Address <--> /wordpress   
`WP_HOME`    <--> `home_url()` <--> `home`    <--> Site Address      <--> /

Now Here is where it gets tricky !

IF you defined those constants in your wp-config.php file, it does not matter what values you have in your database/settings page.
In fact, you will not be able to modify this value through the back end (it\'ll be greyed out). You can still modify in by editing your database, but doing so will have no effect on your site, while the constants exist in your wp-config file.

You config file will not change the values in your database (or hence you settings page). Instead, your database/settings page values will be ignored. The values in wp-config override or take precedence over your database setting.

So... to wrap up (TLDR) :

WP function  | wp_options. | WP constant  | what it represents       | WP Settings Label | Example     
-------------------------------------------------------------------------------------------------------------------------------------
`site_url()` | `siteurl`   | `WP_SITEURL` | WordPress files location | WordPress Address | https://www.example.com/wordpress
`home_url()` | `home`      | `WP_HOME`    | browser address bar      | Site Address      | https://www.example.com 

Where the value for a WP constant takes precedence over an wp_options/ WP Settings value.

The wp_options record value and the WP Settings value are the same.
Editing one, by definition edits the other.
It is just 2 different ways of accessing the same variable.

On the other hand, the WordPress Constants are unique and independent.
Internally, WordPress (PHP) constants override their db counterparts.
If a constant is defined in wp-config, it does not change the database.
But internally WordPress will always prefer/use its value instead of the db one.

SO网友:Milo

如果您希望WP安装在一个目录中,但站点位于域根目录中,则需要移动主索引。php文件输出到域根目录,并编辑require语句以指向目录。

该过程概述如下:Giving WordPress Its Own Directory.

SO网友:Nanhe Kumar

这个site_url()home_url() 功能相似,可能会导致其工作方式混乱。

这个site_url() 函数检索的值siteurlwp_options 数据库中的表。

这是WordPress核心文件的URL
如果核心文件存在于子目录中/wordpress 在web服务器上,值为http://example.com/wordpress.

这个home_url() 函数检索的值homewp_options 数据库中的表。

这是您希望人们访问以查看WordPress网站的地址。

如果WordPress核心文件存在于/wordpress, 但您希望您的网站URL为http://example.com 初始值应为http://example.com.

SO网友:xLRDxREVENGEx

回答第二个问题:

Q: 如果这是正确的,那么我可以让wordpress返回吗http://example.com/ ?

你不能,除非你Giving WordPress its own directory 步骤。使用这意味着您将WordPress核心文件放入/blog/WordPress 然后index.php 进入你的根。

如果您决定将WordPress放在自己的目录中,那么您将使用home_url() 因为要去index.phpsite_url() 获取核心文件等。

参考文献:
Codex for site_url
Codex for home_url
Codex for Giving Wordpress Own Directory

SO网友:jake

获取网站url的最简单方法是不使用任何子目录(http://example.com/ 而不是http://example.com/blog ), 只需使用反斜杠/

例如,如果键入:

<a href="/">domain url</a>
它将创建一个指向您的域的链接

结束

相关推荐

Changing attachment urls?

如何更改附件页URL的格式/[post-url]/[attachment-name]/ 到/media/[attachment-name]/? 我知道我可以覆盖get_attachment_link 通过attachment_link 过滤器,但我想我需要更改重定向结构,以便WordPress知道如何处理这些URL?