返回介绍

sanitize_title()

发布于 2017-09-11 10:19:25 字数 5883 浏览 919 评论 0 收藏 0

sanitize_title( string $title,  string $fallback_title = '',  string $context = 'save' )

Sanitizes a title, or returns a fallback title.


description

Specifically, HTML and PHP tags are stripped. Further actions can be added via the plugin API. If $title is empty and $fallback_title is set, the latter will be used.


参数

$title

(string) (Required) The string to be sanitized.

$fallback_title

(string) (Optional) A title to use if $title is empty.

Default value: ''

$context

(string) (Optional) The operation for which the string is sanitized

Default value: 'save'


返回值

(string) The sanitized string.


源代码

File: wp-includes/formatting.php

function sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
	$raw_title = $title;

	if ( 'save' == $context )
		$title = remove_accents($title);

	/**
	 * Filters a sanitized title string.
	 *
	 * @since 1.2.0
	 *
	 * @param string $title     Sanitized title.
	 * @param string $raw_title The title prior to sanitization.
	 * @param string $context   The context for which the title is being sanitized.
	 */
	$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );

	if ( '' === $title || false === $title )
		$title = $fallback_title;

	return $title;
}

更新日志

Versiondescription
1.0.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: sanitize_title
  • wp-includes/formatting.php: remove_accents()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::prepare_starter_content_attachments()
  • wp-includes/theme.php: wp_update_custom_css_post()
  • wp-includes/theme.php: wp_get_custom_css_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-controller.php: WP_REST_Controller::sanitize_slug()
  • wp-includes/functions.php: wp_parse_slug_list()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::insert_auto_draft_post()
  • wp-includes/class-wp-term-query.php: WP_Term_Query::get_terms()
  • wp-includes/customize/class-wp-customize-nav-menu-setting.php: WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
  • wp-includes/customize/class-wp-customize-nav-menu-setting.php: WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menu_object()
  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::add_category()
  • wp-admin/includes/upgrade.php: wp_install_maybe_enable_pretty_permalinks()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::single_row()
  • wp-admin/includes/ms.php: sync_category_tag_slugs()
  • wp-admin/includes/upgrade.php: make_site_theme()
  • wp-admin/includes/upgrade.php: wp_install_defaults()
  • wp-admin/includes/plugin.php: add_menu_page()
  • wp-admin/includes/media.php: media_handle_upload()
  • wp-admin/includes/media.php: media_handle_sideload()
  • wp-admin/includes/post.php: get_sample_permalink()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_add_hierarchical_term()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_link_category()
  • wp-includes/formatting.php: sanitize_title_for_query()
  • wp-includes/deprecated.php: register_widget_control()
  • wp-includes/deprecated.php: register_sidebar_widget()
  • wp-includes/category.php: get_category_by_path()
  • wp-includes/taxonomy.php: wp_update_term()
  • wp-includes/taxonomy.php: wp_insert_term()
  • wp-includes/taxonomy.php: term_exists()
  • wp-includes/link-template.php: permalink_anchor()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::add_node()
  • wp-includes/user.php: wp_insert_user()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/widgets.php: dynamic_sidebar()
  • wp-includes/widgets.php: is_active_sidebar()
  • Show 31 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 Codex

    WordPress Titles
    To create the file name portion of a URL the same way that WordPress does use this:

    
    <?php
      $new_url = sanitize_title('This Long Title is what My Post or Page might be');
      echo $new_url;
    ?>
    

    It should return a formatted value, the output would be this:

    this-long-title-is-what-my-post-or-page-might-be

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

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

发布评论

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