返回介绍

esc_url()

发布于 2017-09-10 22:29:17 字数 21540 浏览 1504 评论 0 收藏 0

esc_url( string $url,  array $protocols = null,  string $_context = 'display' )

Checks and cleans a URL.


description

A number of characters are removed from the URL. If the URL is for displaying (the default behaviour) ampersands are also replaced. The ‘clean_url’ filter is applied to the returned cleaned URL.


参数

$url

(string) (Required) The URL to be cleaned.

$protocols

(array) (Optional) An array of acceptable protocols. Defaults to return value of wp_allowed_protocols()

Default value: null

$_context

(string) (Optional) Private. Use esc_url_raw() for database usage.

Default value: 'display'


返回值

(string) The cleaned $url after the 'clean_url' filter is applied.


源代码

File: wp-includes/formatting.php

function esc_url( $url, $protocols = null, $_context = 'display' ) {
	$original_url = $url;

	if ( '' == $url )
		return $url;

	$url = str_replace( ' ', '%20', $url );
	$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\[\]\\x80-\\xff]|i', '', $url);

	if ( '' === $url ) {
		return $url;
	}

	if ( 0 !== stripos( $url, 'mailto:' ) ) {
		$strip = array('%0d', '%0a', '%0D', '%0A');
		$url = _deep_replace($strip, $url);
	}

	$url = str_replace(';//', '://', $url);
	/* If the URL doesn't appear to contain a scheme, we
	 * presume it needs http:// prepended (unless a relative
	 * link starting with /,

更新日志

Versiondescription
2.8.0Introduced.

相关函数

Uses

  • wp-includes/http.php: wp_parse_url()
  • wp-includes/formatting.php: _deep_replace()
  • wp-includes/formatting.php: clean_url
  • wp-includes/kses.php: wp_kses_normalize_entities()
  • wp-includes/kses.php: wp_kses_bad_protocol()
  • wp-includes/functions.php: wp_allowed_protocols()
  • wp-includes/plugin.php: apply_filters()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/category-template.php: get_term_parents_list()
  • wp-includes/widgets/class-wp-widget-media-audio.php: WP_Widget_Media_Audio::__construct()
  • wp-includes/widgets/class-wp-widget-media-video.php: WP_Widget_Media_Video::__construct()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-includes/widgets/class-wp-widget-media.php: WP_Widget_Media::__construct()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::__construct()
  • wp-admin/includes/dashboard.php: wp_print_community_events_markup()
  • wp-admin/includes/dashboard.php: wp_dashboard_events_news()
  • wp-includes/theme.php: get_header_video_url()
  • wp-includes/theme.php: the_header_video_url()
  • wp-includes/general-template.php: wp_re源代码_hints()
  • wp-admin/includes/ms.php: network_edit_site_nav()
  • wp-includes/embed.php: the_embed_site_title()
  • wp-includes/general-template.php: get_custom_logo()
  • wp-includes/customize/class-wp-customize-site-icon-control.php: WP_Customize_Site_Icon_Control::content_template()
  • wp-includes/rest-api.php: rest_output_rsd()
  • wp-includes/rest-api.php: rest_output_link_wp_head()
  • wp-includes/embed.php: wp_filter_oembed_result()
  • wp-includes/embed.php: wp_embed_excerpt_more()
  • wp-includes/embed.php: wp_oembed_add_discovery_links()
  • wp-includes/embed.php: get_post_embed_html()
  • wp-includes/author-template.php: get_the_author_posts_link()
  • wp-includes/post-thumbnail-template.php: the_post_thumbnail_url()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::get_edit_link()
  • wp-includes/general-template.php: wp_site_icon()
  • wp-includes/general-template.php: site_icon_url()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::column_name()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::column_blogname()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::column_users()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::handle_row_actions()
  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::column_username()
  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::column_email()
  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::column_blogs()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_author()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_default()
  • wp-includes/customize/class-wp-customize-theme-control.php: WP_Customize_Theme_Control::content_template()
  • wp-admin/includes/theme.php: customize_themes_print_templates()
  • wp-admin/includes/misc.php: wp_admin_canonical_url()
  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::html()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::remove_panel()
  • wp-login.php: login_footer()
  • wp-login.php: login_header()
  • wp-signup.php: signup_another_blog()
  • wp-signup.php: confirm_another_blog_signup()
  • wp-admin/includes/network.php: network_step1()
  • wp-admin/includes/network.php: network_step2()
  • wp-admin/install.php: display_header()
  • wp-admin/includes/theme.php: wp_prepare_themes_for_js()
  • wp-admin/includes/theme.php: get_theme_update_available()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_screen_meta()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::single_row()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::no_items()
  • wp-admin/includes/class-theme-upgrader-skin.php: Theme_Upgrader_Skin::after()
  • wp-admin/includes/class-theme-installer-skin.php: Theme_Installer_Skin::after()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::view_switcher()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::comments_bubble()
  • 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/ms.php: _access_denied_splash()
  • wp-admin/includes/ms.php: site_admin_notice()
  • wp-admin/includes/ms.php: choose_primary_blog()
  • wp-admin/includes/ms.php: update_option_new_admin_email()
  • wp-admin/includes/ms.php: send_confirmation_on_profile_email()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::get_views()
  • wp-admin/includes/misc.php: admin_color_scheme_picker()
  • wp-admin/includes/class-wp-theme-install-list-table.php: WP_Theme_Install_List_Table::install_theme_info()
  • wp-admin/includes/class-wp-theme-install-list-table.php: WP_Theme_Install_List_Table::single_row()
  • wp-admin/includes/class-wp-theme-install-list-table.php: WP_Theme_Install_List_Table::theme_installer_single()
  • wp-admin/includes/update.php: update_nag()
  • wp-admin/includes/update.php: wp_plugin_update_row()
  • wp-admin/includes/update.php: wp_theme_update_row()
  • wp-admin/includes/plugin-install.php: install_dashboard()
  • wp-admin/includes/plugin-install.php: install_plugin_information()
  • wp-admin/includes/deprecated.php: wp_dashboard_plugins_output()
  • wp-admin/includes/dashboard.php: wp_dashboard_quota()
  • wp-admin/includes/dashboard.php: wp_dashboard_browser_nag()
  • wp-admin/includes/dashboard.php: wp_dashboard_quick_press()
  • wp-admin/includes/dashboard.php: wp_dashboard_recent_drafts()
  • wp-admin/includes/dashboard.php: _wp_dashboard_recent_comments_row()
  • wp-admin/includes/dashboard.php: wp_add_dashboard_widget()
  • wp-admin/includes/plugin.php: menu_page_url()
  • wp-admin/includes/upgrade.php: wp_install_defaults()
  • wp-admin/includes/plugin.php: _get_plugin_data_markup_translate()
  • wp-admin/includes/class-wp-plugin-install-list-table.php: WP_Plugin_Install_List_Table::display_rows()
  • wp-admin/includes/template.php: wp_import_upload_form()
  • wp-admin/includes/class-wp-themes-list-table.php: WP_Themes_List_Table::display_rows()
  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::single_row()
  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::get_views()
  • wp-admin/includes/media.php: media_upload_type_form()
  • wp-admin/includes/media.php: media_upload_type_url_form()
  • wp-admin/includes/media.php: media_upload_gallery_form()
  • wp-admin/includes/media.php: media_upload_library_form()
  • wp-admin/includes/media.php: media_upload_max_image_resize()
  • wp-admin/includes/media.php: wp_media_upload_handler()
  • wp-admin/includes/media.php: the_media_upload_tabs()
  • wp-admin/includes/post.php: get_sample_permalink_html()
  • wp-admin/includes/post.php: _wp_post_thumbnail_html()
  • wp-admin/includes/post.php: _admin_notice_post_locked()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_attachment_to_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_link_to_editor()
  • wp-admin/includes/meta-boxes.php: post_submit_meta_box()
  • wp-admin/includes/bookmark.php: edit_link()
  • wp-admin/includes/bookmark.php: get_default_link_to_edit()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_author()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_date()
  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::column_comment()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::column_name()
  • wp-admin/includes/class-wp-terms-list-table.php: WP_Terms_List_Table::column_posts()
  • wp-admin/includes/class-walker-nav-menu-edit.php: Walker_Nav_Menu_Edit::start_el()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_item_post_type_meta_box()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_item_taxonomy_meta_box()
  • wp-admin/includes/file.php: request_filesystem_credentials()
  • wp-admin/includes/widgets.php: wp_widget_control()
  • wp-admin/includes/comment.php: get_comment_to_edit()
  • wp-admin/includes/credits.php: _wp_credits_add_profile_link()
  • wp-admin/includes/credits.php: _wp_credits_build_object_link()
  • wp-admin/custom-header.php: Custom_Image_Header::step_1()
  • wp-admin/custom-header.php: Custom_Image_Header::step_2()
  • wp-admin/includes/ms.php: confirm_delete_users()
  • wp-admin/update-core.php: core_upgrade_preamble()
  • wp-admin/update-core.php: list_plugin_updates()
  • wp-admin/update-core.php: list_theme_updates()
  • wp-admin/update-core.php: list_translation_updates()
  • wp-admin/update-core.php: do_core_upgrade()
  • wp-admin/menu-header.php: _wp_menu_output()
  • wp-admin/custom-background.php: Custom_Background::admin_page()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::register_controls()
  • wp-includes/class.wp-styles.php: WP_Styles::_css_href()
  • wp-includes/category-template.php: get_the_term_list()
  • wp-includes/class-walker-category.php: Walker_Category::start_el()
  • wp-includes/category-template.php: wp_generate_tag_cloud()
  • wp-includes/theme.php: wp_customize_url()
  • wp-includes/category-template.php: get_the_category_list()
  • wp-includes/category-template.php: wp_list_categories()
  • wp-includes/theme.php: _wp_customize_loader_settings()
  • wp-includes/theme.php: header_image()
  • wp-includes/formatting.php: esc_url_raw()
  • wp-includes/formatting.php: _make_url_clickable_cb()
  • wp-includes/formatting.php: _make_web_ftp_clickable_cb()
  • wp-includes/formatting.php: translate_smiley()
  • wp-includes/pluggable.php: get_avatar()
  • wp-includes/general-template.php: paginate_links()
  • wp-includes/general-template.php: wp_admin_css()
  • wp-includes/general-template.php: feed_links()
  • wp-includes/general-template.php: feed_links_extra()
  • wp-includes/general-template.php: rsd_link()
  • wp-includes/general-template.php: get_archives_link()
  • wp-includes/general-template.php: wp_loginout()
  • wp-includes/general-template.php: wp_login_form()
  • wp-includes/general-template.php: wp_register()
  • wp-includes/general-template.php: get_search_form()
  • wp-includes/deprecated.php: get_index_rel_link()
  • wp-includes/deprecated.php: clean_url()
  • wp-includes/deprecated.php: comments_rss()
  • wp-includes/deprecated.php: get_links()
  • wp-includes/class-wp-theme.php: WP_Theme::markup_header()
  • wp-includes/functions.php: wp_auth_check_html()
  • wp-includes/functions.php: wp_nonce_ays()
  • wp-includes/widgets/class-wp-widget-rss.php: WP_Widget_RSS::widget()
  • wp-includes/widgets/class-wp-widget-recent-comments.php: WP_Widget_Recent_Comments::widget()
  • wp-includes/widgets/class-wp-widget-meta.php: WP_Widget_Meta::widget()
  • wp-includes/widgets.php: wp_widget_rss_output()
  • wp-includes/widgets.php: wp_widget_rss_form()
  • wp-includes/widgets.php: wp_widget_rss_process()
  • wp-includes/class-wp-embed.php: WP_Embed::maybe_make_link()
  • wp-includes/link-template.php: rel_canonical()
  • wp-includes/link-template.php: wp_shortlink_wp_head()
  • wp-includes/link-template.php: the_shortlink()
  • wp-includes/link-template.php: get_next_comments_link()
  • wp-includes/link-template.php: get_previous_comments_link()
  • wp-includes/link-template.php: get_pagenum_link()
  • wp-includes/link-template.php: next_posts()
  • wp-includes/link-template.php: previous_posts()
  • wp-includes/link-template.php: edit_post_link()
  • wp-includes/link-template.php: edit_comment_link()
  • wp-includes/link-template.php: edit_bookmark_link()
  • wp-includes/link-template.php: the_feed_link()
  • wp-includes/link-template.php: post_comments_feed_link()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::_render()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::_render_item()
  • wp-includes/link-template.php: the_permalink()
  • wp-includes/update.php: wp_version_check()
  • wp-includes/class-oembed.php: WP_oEmbed::data2html()
  • wp-includes/admin-bar.php: wp_admin_bar_edit_menu()
  • wp-includes/admin-bar.php: wp_admin_bar_search_menu()
  • wp-includes/feed.php: self_link()
  • wp-includes/feed.php: the_permalink_rss()
  • wp-includes/feed.php: comments_link_feed()
  • wp-includes/feed.php: comment_guid()
  • wp-includes/feed.php: comment_link()
  • wp-includes/user.php: sanitize_user_field()
  • wp-includes/bookmark-template.php: _walk_bookmarks()
  • wp-includes/class-walker-nav-menu.php: Walker_Nav_Menu::start_el()
  • wp-includes/post-template.php: wp_get_attachment_link()
  • wp-includes/post-template.php: get_the_password_form()
  • wp-includes/post-template.php: _wp_link_page()
  • wp-includes/embed.php: wp_embed_handler_audio()
  • wp-includes/embed.php: wp_embed_handler_video()
  • wp-includes/media.php: wp_video_shortcode()
  • wp-includes/media.php: wp_mediaelement_fallback()
  • wp-includes/media.php: wp_audio_shortcode()
  • wp-includes/ms-functions.php: newblog_notify_siteadmin()
  • wp-includes/ms-functions.php: newuser_notify_siteadmin()
  • wp-includes/ms-functions.php: wpmu_signup_blog_notification()
  • wp-includes/ms-deprecated.php: get_most_active_blogs()
  • wp-includes/class.wp-scripts.php: WP_Scripts::do_item()
  • wp-includes/author-template.php: get_the_author_link()
  • wp-includes/author-template.php: wp_list_authors()
  • wp-includes/ms-blogs.php: get_blogaddress_by_name()
  • wp-includes/ms-blogs.php: get_blogaddress_by_id()
  • wp-includes/rss.php: wp_rss()
  • wp-includes/class-walker-comment.php: Walker_Comment::comment()
  • wp-includes/class-walker-comment.php: Walker_Comment::html5_comment()
  • wp-includes/comment-template.php: comment_form()
  • wp-includes/comment-template.php: comments_template()
  • wp-includes/comment-template.php: get_comment_reply_link()
  • wp-includes/comment-template.php: comments_link()
  • wp-includes/comment-template.php: get_comment_author_email_link()
  • wp-includes/comment-template.php: get_comment_author_url()
  • wp-includes/comment.php: wp_set_comment_cookies()
  • wp-includes/media-template.php: wp_print_media_templates()
  • Show 220 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: 1You must log in to vote on the helpfulness of this note Contributed by Elio Rivero

    If the URI protocol is not one of the allowed protocols, the result of esc_url() is an empty string. The list of default protocols allowed by WordPress can be extended with the following code:

    <?php
    /**
     * Extend list of allowed protocols.
     *
     * @param array $protocols List of default protocols allowed by WordPress.
     *
     * @return array $protocols Updated list including new protocols.
     */
    function wporg_extend_allowed_protocols( $protocols ){
    	$protocols[] = 'skype';
    	$protocols[] = 'spotify';
    	$protocols[] = 'macappstores';
    	return $protocols;
    }
    add_filter( 'kses_allowed_protocols' , 'wporg_extend_allowed_protocols' );
    ?>
  2. Adding a link to home
    As featured in the Twenty Thirteen theme, although simplified for the sake of the example

    
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a>
    
    

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

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

发布评论

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