返回介绍

set_url_scheme()

发布于 2017-09-11 10:22:40 字数 7450 浏览 1358 评论 0 收藏 0

set_url_scheme( string $url,  string|null $scheme = null )

Sets the scheme for a URL.


description


参数

$url

(string) (Required) Absolute URL that includes a scheme

$scheme

(string|null) (Optional) Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.

Default value: null


返回值

(string) $url URL with chosen scheme.


源代码

File: wp-includes/link-template.php

function set_url_scheme( $url, $scheme = null ) {
	$orig_scheme = $scheme;

	if ( ! $scheme ) {
		$scheme = is_ssl() ? 'https' : 'http';
	} elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
		$scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
	} elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
		$scheme = is_ssl() ? 'https' : 'http';
	}

	$url = trim( $url );
	if ( substr( $url, 0, 2 ) === '//' )
		$url = 'http:' . $url;

	if ( 'relative' == $scheme ) {
		$url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
		if ( $url !== '' && $url[0] === '/' )
			$url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
	} else {
		$url = preg_replace( '#^\w+://#', $scheme . '://', $url );
	}

	/**
	 * Filters the resulting URL after setting the scheme.
	 *
	 * @since 3.4.0
	 *
	 * @param string      $url         The complete URL including scheme and path.
	 * @param string      $scheme      Scheme applied to the URL. One of 'http', 'https', or 'relative'.
	 * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
	 *                                 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
	 */
	return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
}

更新日志

Versiondescription
4.4.0The 'rest' scheme was added.
3.4.0Introduced.

相关函数

Uses

  • wp-includes/load.php: is_ssl()
  • wp-includes/functions.php: force_ssl_admin()
  • wp-includes/link-template.php: set_url_scheme
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/theme.php: get_header_video_url()
  • wp-includes/rest-api.php: get_rest_url()
  • wp-includes/media.php: wp_calculate_image_srcset()
  • wp-includes/link-template.php: get_preview_post_link()
  • wp-includes/customize/class-wp-customize-theme-control.php: WP_Customize_Theme_Control::content_template()
  • wp-includes/link-template.php: get_avatar_data()
  • wp-admin/includes/misc.php: wp_admin_canonical_url()
  • wp-admin/includes/translation-install.php: translations_api()
  • wp-admin/includes/theme.php: themes_api()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::pagination()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::print_column_headers()
  • wp-admin/includes/update.php: get_core_checksums()
  • wp-admin/includes/plugin-install.php: plugins_api()
  • wp-admin/includes/plugin.php: add_menu_page()
  • wp-admin/includes/media.php: edit_form_image_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_themes()
  • wp-admin/includes/file.php: get_home_path()
  • wp-admin/custom-header.php: Custom_Image_Header::show_header_selector()
  • wp-admin/custom-background.php: Custom_Background::admin_page()
  • wp-includes/theme.php: _custom_background_cb()
  • wp-includes/theme.php: get_header_image()
  • wp-includes/formatting.php: wp_rel_nofollow_callback()
  • wp-includes/pluggable.php: auth_redirect()
  • wp-includes/deprecated.php: url_is_accessable_via_ssl()
  • wp-includes/link-template.php: content_url()
  • wp-includes/link-template.php: plugins_url()
  • wp-includes/link-template.php: network_site_url()
  • wp-includes/link-template.php: network_home_url()
  • wp-includes/link-template.php: get_home_url()
  • wp-includes/link-template.php: get_site_url()
  • wp-includes/update.php: wp_version_check()
  • wp-includes/update.php: wp_update_plugins()
  • wp-includes/update.php: wp_update_themes()
  • wp-includes/feed.php: self_link()
  • wp-includes/nav-menu-template.php: _wp_menu_item_classes_by_context()
  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/rewrite.php: url_to_postid()
  • wp-includes/ms-functions.php: filter_SSL()
  • wp-includes/ms-functions.php: install_blog()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_multisite_getUsersBlogs()
  • wp-includes/class-wp-editor.php: _WP_Editors::editor_settings()
  • Show 37 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Drew Jaynes

    Important Note: set_url_scheme() does NOT add a scheme to a bare URL. If you pass in ‘example.org/what/ever’, you’ll get ‘example.org/what/ever’ out the other side. For this reason, you should always add a basic scheme to URLs if you know the input URL won’t have one, e.g. ‘https://’.

    
    $url = 'example.org/what/ever/'
    print_r( set_url_scheme( $url, 'https' ) );
    // Result: 'example.org/what/ever'
    
    print_r( set_url_scheme( 'http://' . $url, 'https' ) );
    // Result: 'https://example.org/what/ever ('https' if is_ssl() is true, otherwise 'http')
    
  2. Usage with is_ssl()

    One of the nice things about set_url_scheme() is that if you’re in an SSL environment and everything is working properly, you don’t necessarily need to define a scheme, as set_url_scheme() will do that for you.

    For example:

    
    $url = 'http://example.org/some/permalink';
    
    print_r( set_url_scheme( $url ) );
    // If is_ssl() is true:
    // Result: 'https://example.org/some/permalink
    //
    // If is_ssl() is false:
    // Result: 'http://example.org/some/permalink (no change)
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文