home_url()
概述
home_url() 函数用于获取当前站点前端可访问的 URL,基于 'home' 选项并自动处理协议(如 HTTPS)。它支持附加路径和指定协议方案,是 WordPress 中获取主页链接的核心函数。
关键要点
- 返回当前站点的主页 URL,协议根据 is_ssl() 自动确定,但可通过 $scheme 参数覆盖
- 接受两个可选参数:$path(相对路径)和 $scheme(协议方案,如 'http'、'https'、'relative'、'rest' 或 null)
- 内部调用 get_home_url() 函数,返回字符串类型的 URL
- 广泛用于生成各种链接,如站点地图、自定义器 URL、RSS 源等
代码示例
$url = home_url();
echo $url;
// 输出: http://www.example.com
$url = home_url( '/' );
echo $url;
// 输出: http://www.example.com/
$url = home_url( $path = '/', $scheme = 'https' );
echo $url;
// 输出: https://www.example.com/
$url = home_url( $path = 'example', $scheme = 'relative' );
echo $url;
// 输出: /example注意事项
- URL 默认不带尾部斜杠,需手动添加如 home_url( '/' )
- 建议使用 esc_url() 对输出进行转义以确保安全性
- 自 WordPress 3.0.0 版本引入,是许多其他链接函数的基础
Retrieves the URL for the current site where the front end is accessible.
Description
Returns the ‘home’ option with the appropriate protocol. The protocol will be ‘https’ if is_ssl() evaluates to true; otherwise, it will be the same as the ‘home’ option.
If $scheme is ‘http’ or ‘https’, is_ssl() is overridden.
Parameters
$pathstringoptional-
Path relative to the home URL. Default empty.
$schemestring|nulloptional-
Scheme to give the home URL context. Accepts
'http','https','relative','rest', or null.Default:
null
Return
string Home URL link with optional path appended.
Source
function home_url( $path = '', $scheme = null ) {
return get_home_url( null, $path, $scheme );
}
Related
| Uses | Description |
|---|---|
get_home_url()wp-includes/link-template.php |
Retrieves the URL for a given site where the front end is accessible. |
| Used by | Description |
|---|---|
WP_URL_Pattern_Prefixer::get_default_contexts()wp-includes/class-wp-url-pattern-prefixer.php |
Returns the default contexts used by the class. |
WP_Automatic_Updater::has_fatal_error()wp-admin/includes/class-wp-automatic-updater.php |
Performs a loopback request to check for potential fatal errors. |
wp_get_https_detection_errors()wp-includes/https-detection.php |
Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors. |
wp_internal_hosts()wp-includes/link-template.php |
Returns an array of URL hosts which are considered to be internal hosts. |
WP_Site_Health::check_for_page_caching()wp-admin/includes/class-wp-site-health.php |
Checks if site has page cache enabled or not. |
wp_is_home_url_using_https()wp-includes/https-detection.php |
Checks whether the current site URL is using HTTPS. |
wp_should_replace_insecure_home_url()wp-includes/https-migration.php |
Checks whether WordPress should replace old HTTP URLs to the site with their HTTPS counterpart. |
wp_replace_insecure_home_url()wp-includes/https-migration.php |
Replaces insecure HTTP URLs to the site in the given content, if configured to do so. |
WP_Sitemaps_Provider::get_sitemap_url()wp-includes/sitemaps/class-wp-sitemaps-provider.php |
Gets the URL of a sitemap entry. |
WP_Sitemaps_Index::get_index_url()wp-includes/sitemaps/class-wp-sitemaps-index.php |
Builds the URL for the sitemap index. |
WP_Sitemaps_Renderer::get_sitemap_index_stylesheet_url()wp-includes/sitemaps/class-wp-sitemaps-renderer.php |
Gets the URL for the sitemap index stylesheet. |
WP_Sitemaps_Renderer::get_sitemap_stylesheet_url()wp-includes/sitemaps/class-wp-sitemaps-renderer.php |
Gets the URL for the sitemap stylesheet. |
WP_Sitemaps_Posts::get_url_list()wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php |
Gets a URL list for a post type sitemap. |
WP_Automatic_Updater::send_plugin_theme_email()wp-admin/includes/class-wp-automatic-updater.php |
Sends an email upon the completion or failure of a plugin or theme background update. |
get_self_link()wp-includes/feed.php |
Returns the link for the currently displayed feed. |
WP_Recovery_Mode::handle_exit_recovery_mode()wp-includes/class-wp-recovery-mode.php |
Handles a request to exit Recovery Mode. |
WP_Recovery_Mode_Email_Service::send_recovery_mode_email()wp-includes/class-wp-recovery-mode-email-service.php |
Sends the Recovery Mode email to the site admin email address. |
_wp_privacy_send_request_confirmation_notification()wp-includes/user.php |
Notifies the site administrator via email when a request is confirmed. |
_wp_privacy_send_erasure_fulfillment_notification()wp-includes/user.php |
Notifies the user when their erasure request is fulfilled. |
wp_send_user_request()wp-includes/user.php |
Send a confirmation request email to confirm an action. |
wp_privacy_send_personal_data_export_email()wp-admin/includes/privacy-tools.php |
Send an email to the user with a link to the personal data export file |
wp_site_admin_email_change_notification()wp-includes/functions.php |
Sends an email to the old site admin email address when the site admin email address changes. |
wp_network_admin_email_change_notification()wp-includes/ms-functions.php |
Sends an email to the old network admin email address when the network admin email address changes. |
wp_edit_theme_plugin_file()wp-admin/includes/file.php |
Attempts to edit a file for a theme or plugin. |
WP_Community_Events::get_events()wp-admin/includes/class-wp-community-events.php |
Gets data about events near a particular location. |
WP_Customize_Manager::is_cross_domain()wp-includes/class-wp-customize-manager.php |
Determines whether the admin and the frontend are on different domains. |
WP_Customize_Manager::get_allowed_urls()wp-includes/class-wp-customize-manager.php |
Gets URLs allowed to be previewed. |
get_theme_starter_content()wp-includes/theme.php |
Expands a theme’s starter content configuration using core-provided data. |
the_embed_site_title()wp-includes/embed.php |
Prints the necessary markup for the site title in an embed template. |
get_custom_logo()wp-includes/general-template.php |
Returns a custom logo, linked to home unless the theme supports removing the link on the home page. |
get_post_embed_url()wp-includes/embed.php |
Retrieves the URL to embed a specific post in an iframe. |
WP_Customize_Manager::get_preview_url()wp-includes/class-wp-customize-manager.php |
Gets the initial URL to be previewed. |
WP_Customize_Manager::get_return_url()wp-includes/class-wp-customize-manager.php |
Gets URL to link the user to when closing the Customizer. |
WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php |
Prints JavaScript settings for parent window. |
WP_Customize_Manager::set_preview_url()wp-includes/class-wp-customize-manager.php |
Sets the initial URL to be previewed. |
WP_REST_Server::get_index()wp-includes/rest-api/class-wp-rest-server.php |
Retrieves the site index. |
WP_Customize_Nav_Menus::search_available_items_query()wp-includes/class-wp-customize-nav-menus.php |
Performs post queries for available-item searching. |
WP_Customize_Nav_Menus::load_available_items_query()wp-includes/class-wp-customize-nav-menus.php |
Performs the post_type and taxonomy queries for loading available menu items. |
login_footer()wp-login.php |
Outputs the footer for the login page. |
confirm_another_blog_signup()wp-signup.php |
Shows a message confirming that the new site has been created. |
WP_Automatic_Updater::send_email()wp-admin/includes/class-wp-automatic-updater.php |
Sends an email upon the completion or failure of a background core update. |
WP_Automatic_Updater::send_debug_email()wp-admin/includes/class-wp-automatic-updater.php |
Prepares and sends an email of a full log of background update results, useful for debugging and geekery. |
themes_api()wp-admin/includes/theme.php |
Retrieves theme installer pages from the WordPress.org Themes API. |
update_option_new_admin_email()wp-admin/includes/misc.php |
Sends a confirmation request email when a change of site admin email address is attempted. |
send_confirmation_on_profile_email()wp-includes/user.php |
Sends a confirmation request email when a change of user email address is attempted. |
wp_check_browser_version()wp-admin/includes/dashboard.php |
Checks if the user needs a browser update. |
plugins_api()wp-admin/includes/plugin-install.php |
Retrieves plugin installer pages from the WordPress.org Plugins API. |
wp_nav_menu_item_post_type_meta_box()wp-admin/includes/nav-menu.php |
Displays a meta box for a post type menu item. |
wp_get_popular_importers()wp-admin/includes/import.php |
Returns a list from WordPress.org of popular importer plugins. |
admin_created_user_email()wp-admin/includes/user.php |
|
wp_credits()wp-admin/includes/credits.php |
Retrieves the contributor credits. |
Custom_Image_Header::step_1()wp-admin/includes/class-custom-image-header.php |
Displays first step of custom header image page. |
Custom_Background::admin_page()wp-admin/includes/class-custom-background.php |
Displays the custom background page. |
WP_Customize_Manager::customize_preview_settings()wp-includes/class-wp-customize-manager.php |
Prints JavaScript settings for preview frame. |
wp_list_categories()wp-includes/category-template.php |
Displays or retrieves the HTML list of categories. |
wp_customize_support_script()wp-includes/theme.php |
Prints a script to check whether or not the Customizer is supported, and apply either the no-customize-support or customize-support class to the body. |
_wp_customize_loader_settings()wp-includes/theme.php |
Adds settings for the customize-loader script. |
wp_validate_redirect()wp-includes/pluggable.php |
Validates a URL for use in a redirect. |
wp_get_archives()wp-includes/general-template.php |
Displays archive links based on type and format. |
get_bloginfo()wp-includes/general-template.php |
Retrieves information about the current site. |
get_search_form()wp-includes/general-template.php |
Displays search form. |
wp_admin_bar_dashboard_view_site_menu()wp-includes/deprecated.php |
Add the “Dashboard”/”Visit Site” menu. |
WP::parse_request()wp-includes/class-wp.php |
Parses the request to find the correct WordPress query. |
wp_get_referer()wp-includes/functions.php |
Retrieves referer from ‘_wp_http_referer’ or HTTP referer. |
WP_Widget_RSS::widget()wp-includes/widgets/class-wp-widget-rss.php |
Outputs the content for the current RSS widget instance. |
WP_Widget_Categories::widget()wp-includes/widgets/class-wp-widget-categories.php |
Outputs the content for the current Categories widget instance. |
get_term_link()wp-includes/taxonomy.php |
Generates a permalink for a taxonomy term archive. |
wp_get_shortlink()wp-includes/link-template.php |
Returns a shortlink for a post, page, attachment, or site. |
network_home_url()wp-includes/link-template.php |
Retrieves the home URL for the current network. |
get_pagenum_link()wp-includes/link-template.php |
Retrieves the link for a page number. |
get_post_type_archive_link()wp-includes/link-template.php |
Retrieves the permalink for a post type archive. |
get_term_feed_link()wp-includes/link-template.php |
Retrieves the feed link for a term. |
get_search_link()wp-includes/link-template.php |
Retrieves the permalink for a search. |
get_month_link()wp-includes/link-template.php |
Retrieves the permalink for the month archives with year. |
get_day_link()wp-includes/link-template.php |
Retrieves the permalink for the day archives with year and month. |
get_feed_link()wp-includes/link-template.php |
Retrieves the permalink for the feed type. |
get_post_comments_feed_link()wp-includes/link-template.php |
Retrieves the permalink for the post comments feed. |
get_author_feed_link()wp-includes/link-template.php |
Retrieves the feed link for a given author. |
get_permalink()wp-includes/link-template.php |
Retrieves the full permalink for the current post or post ID. |
get_post_permalink()wp-includes/link-template.php |
Retrieves the permalink for a post of a custom post type. |
get_page_link()wp-includes/link-template.php |
Retrieves the permalink for the current page or page ID. |
_get_page_link()wp-includes/link-template.php |
Retrieves the page permalink. |
get_attachment_link()wp-includes/link-template.php |
Retrieves the permalink for an attachment. |
get_year_link()wp-includes/link-template.php |
Retrieves the permalink for the year archives. |
WP_Admin_Bar::initialize()wp-includes/class-wp-admin-bar.php |
Initializes the admin bar. |
wp_version_check()wp-includes/update.php |
Checks WordPress version against the newest version. |
wp_update_plugins()wp-includes/update.php |
Checks for available updates to plugins based on the latest versions hosted on WordPress.org. |
wp_update_themes()wp-includes/update.php |
Checks for available updates to themes based on the latest versions hosted on WordPress.org. |
get_allowed_http_origins()wp-includes/http.php |
Retrieves list of allowed HTTP origins. |
WP_oEmbed::__construct()wp-includes/class-wp-oembed.php |
Constructor. |
wp_admin_bar_site_menu()wp-includes/admin-bar.php |
Adds the “Site Name” menu. |
wp_admin_bar_my_sites_menu()wp-includes/admin-bar.php |
Adds the “My Sites/[Site Name]” menu and all submenus. |
wp_admin_bar_search_menu()wp-includes/admin-bar.php |
Adds search form. |
wp_update_user()wp-includes/user.php |
Updates a user in the database. |
_wp_menu_item_classes_by_context()wp-includes/nav-menu-template.php |
Adds the class property classes for the current context, if applicable. |
wp_page_menu()wp-includes/post-template.php |
Displays or retrieves a list of pages with an optional home link. |
is_local_attachment()wp-includes/post.php |
Determines whether an attachment URI is local and really an attachment. |
WP_Rewrite::mod_rewrite_rules()wp-includes/class-wp-rewrite.php |
Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess. |
WP_Rewrite::iis7_url_rewrite_rules()wp-includes/class-wp-rewrite.php |
Retrieves IIS7 URL Rewrite formatted rewrite rules to write to web.config file. |
WP_Rewrite::rewrite_rules()wp-includes/class-wp-rewrite.php |
Constructs rewrite matches and queries from permalink structure. |
redirect_canonical()wp-includes/canonical.php |
Redirects incoming links to the proper URL based on the site url. |
wp_redirect_admin_locations()wp-includes/canonical.php |
Redirects a variety of shorthand URLs to the admin. |
url_to_postid()wp-includes/rewrite.php |
Examines a URL and try to determine the post ID it represents. |
maybe_add_existing_user_to_blog()wp-includes/ms-functions.php |
Adds a new user to a blog by visiting /newbloguser/{key}/. |
get_author_posts_url()wp-includes/author-template.php |
Retrieves the URL to the author page for the user with the ID provided. |
wp_xmlrpc_server::wp_getUsersBlogs()wp-includes/class-wp-xmlrpc-server.php |
Retrieves the blogs of the user. |
weblog_ping()wp-includes/comment.php |
Sends a pingback. |
wp_set_comment_cookies()wp-includes/comment.php |
Sets the cookies used to store an unauthenticated commentator’s identity. Typically used to recall previous comments by this commentator that are still held in moderation. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 3 content
Codex
Example
$url = home_url(); echo $url;Output:
http://www.example.com(Note the lack of a trailing slash)
$url = home_url( '/' ); echo $url;Output:
http://www.example.com/$url = home_url( $path = '/', $scheme = 'https' ); echo $url;Output:
https://www.example.com/$url = home_url( $path = 'example', $scheme = 'relative' ); echo $url;Output:
/exampleSkip to note 4 content
Aurovrata Venet
You can also add attributes at the end of the `$path` is need be…
$url = esc_url(home_url( '/my-page?id=123')); echo $url; // <a href="https://your-domain/my-page?id=123" rel="nofollow ugc">https://your-domain/my-page?id=123</a>but best to use esc_url() to ensure you get a clean url