trailingslashit()
概述
trailingslashit() 是 WordPress 核心函数,用于在字符串末尾添加一个正斜杠,主要用于路径处理。它会先移除现有的尾部斜杠,再添加一个正斜杠,避免重复斜杠问题。
关键要点
- 函数功能:向字符串添加尾部正斜杠,并自动处理现有斜杠以防止重复
- 主要用途:适用于路径处理,但也可用于其他字符串场景
- 参数:接受一个必需字符串参数 $value,返回添加斜杠后的字符串
- 内部实现:基于 untrailingslashit() 函数构建,确保简洁高效
代码示例
// 基本用法示例
$path = trailingslashit( '/home/julien/bin/dotfiles' );
// $path 现在为: /home/julien/bin/dotfiles/
// 在 WordPress 开发中的实际应用
wp_enqueue_style( 'main-css', trailingslashit( get_template_directory_uri() ) . 'style.css' );
require trailingslashit( get_template_directory() ) . 'inc/custom-theme-functions.php';注意事项
- 该函数不提供特定路径支持,开发者需确保输入值适合路径使用
- 与 untrailingslashit() 函数配合使用,可灵活处理路径斜杠问题
- 在 WordPress 核心和插件/主题开发中广泛使用,涉及文件系统、URL 生成等多个场景
Appends a trailing slash.
Description
Will remove trailing forward and backslashes if it exists already before adding a trailing forward slash. This prevents double slashing a string or path.
The primary use of this is for paths and thus should be used for paths. It is not restricted to paths and offers no specific path support.
Parameters
$valuestringrequired-
Value to which trailing slash will be added.
Return
string String with trailing slash added.
Source
function trailingslashit( $value ) {
return untrailingslashit( $value ) . '/';
}
Related
| Uses | Description |
|---|---|
untrailingslashit()wp-includes/formatting.php |
Removes trailing forward slashes and backslashes if they exist. |
| Used by | Description |
|---|---|
WP_Block_Metadata_Registry::get_default_collection_roots()wp-includes/class-wp-block-metadata-registry.php |
Gets the default collection root directory paths. |
WP_URL_Pattern_Prefixer::get_default_contexts()wp-includes/class-wp-url-pattern-prefixer.php |
Returns the default contexts used by the class. |
WP_URL_Pattern_Prefixer::__construct()wp-includes/class-wp-url-pattern-prefixer.php |
Constructor. |
get_block_asset_url()wp-includes/blocks.php |
Gets the URL to a block asset. |
WP_Theme::get_block_patterns()wp-includes/class-wp-theme.php |
Gets block pattern data for a specified theme. |
WP_Upgrader::move_to_temp_backup_dir()wp-admin/includes/class-wp-upgrader.php |
Moves the plugin or theme being updated into a temporary backup directory. |
WP_Upgrader::restore_temp_backup()wp-admin/includes/class-wp-upgrader.php |
Restores the plugin or theme from temporary backup. |
wp_opcache_invalidate_directory()wp-admin/includes/file.php |
Attempts to clear the opcode cache for a directory of files. |
move_dir()wp-admin/includes/file.php |
Moves a directory from one location to another. |
WP_REST_Global_Styles_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Prepares links for the request. |
WP_REST_Menu_Locations_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php |
Prepares links for the request. |
WP_REST_Server::match_request_to_handler()wp-includes/rest-api/class-wp-rest-server.php |
Matches a request object to its handler. |
wp_image_file_matches_image_meta()wp-includes/media.php |
Determines if the image meta data is for the image source file. |
register_block_type_from_metadata()wp-includes/blocks.php |
Registers a block type from the metadata stored in the |
WP_Site_Health::wp_cron_scheduled_check()wp-admin/includes/class-wp-site-health.php |
Runs the scheduled event to check and update the latest site health status for the website. |
wp_normalize_site_data()wp-includes/ms-site.php |
Normalizes data for a site prior to inserting or updating in the database. |
load_script_textdomain()wp-includes/l10n.php |
Loads the script translated strings. |
wp_privacy_exports_dir()wp-includes/functions.php |
Returns the directory used to store personal data export files. |
wp_privacy_exports_url()wp-includes/functions.php |
Returns the URL of the directory used to store personal data export files. |
wp_delete_file_from_directory()wp-includes/functions.php |
Deletes a file if its path is within the given directory. |
_load_textdomain_just_in_time()wp-includes/l10n.php |
Loads plugin and theme text domains just-in-time. |
wp_get_canonical_url()wp-includes/link-template.php |
Returns the canonical URL for a post. |
_wp_upload_dir()wp-includes/functions.php |
A non-filtered, non-cached version of wp_upload_dir() that doesn’t check the path. |
get_rest_url()wp-includes/rest-api.php |
Retrieves the URL to a REST endpoint on a site. |
get_post_embed_url()wp-includes/embed.php |
Retrieves the URL to embed a specific post in an iframe. |
wp_calculate_image_srcset()wp-includes/media.php |
A helper function to calculate the image sources to include in a ‘srcset’ attribute. |
_upgrade_422_remove_genericons()wp-admin/includes/update-core.php |
Cleans up Genericons example files. |
network_step2()wp-admin/includes/network.php |
Prints step 2 for Network installation process. |
Core_Upgrader::upgrade()wp-admin/includes/class-core-upgrader.php |
Upgrades WordPress core. |
Theme_Upgrader::delete_old_theme()wp-admin/includes/class-theme-upgrader.php |
Deletes the old theme during an upgrade. |
Plugin_Upgrader::check_package()wp-admin/includes/class-plugin-upgrader.php |
Checks that the source package contains a valid plugin. |
Plugin_Upgrader::delete_old_plugin()wp-admin/includes/class-plugin-upgrader.php |
Deletes the old plugin during an upgrade. |
Theme_Upgrader::check_package()wp-admin/includes/class-theme-upgrader.php |
Checks that the package source contains a valid theme. |
WP_Upgrader::install_package()wp-admin/includes/class-wp-upgrader.php |
Install a package. |
WP_Filesystem_SSH2::dirlist()wp-admin/includes/class-wp-filesystem-ssh2.php |
Gets details for files in a directory or a specific file. |
WP_Filesystem_SSH2::cwd()wp-admin/includes/class-wp-filesystem-ssh2.php |
Gets the current working directory. |
delete_theme()wp-admin/includes/theme.php |
Removes a theme. |
WP_Filesystem_FTPext::is_dir()wp-admin/includes/class-wp-filesystem-ftpext.php |
Checks if resource is a directory. |
WP_Filesystem_FTPext::dirlist()wp-admin/includes/class-wp-filesystem-ftpext.php |
Gets details for files in a directory or a specific file. |
WP_Filesystem_FTPext::cwd()wp-admin/includes/class-wp-filesystem-ftpext.php |
Gets the current working directory. |
WP_Filesystem_FTPext::delete()wp-admin/includes/class-wp-filesystem-ftpext.php |
Deletes a file or directory. |
WP_Filesystem_Base::find_folder()wp-admin/includes/class-wp-filesystem-base.php |
Locates a folder on the remote filesystem. |
WP_Filesystem_Base::search_for_folder()wp-admin/includes/class-wp-filesystem-base.php |
Locates a folder on the remote filesystem. |
WP_Filesystem_Direct::dirlist()wp-admin/includes/class-wp-filesystem-direct.php |
Gets details for files in a directory or a specific file. |
WP_Filesystem_Direct::delete()wp-admin/includes/class-wp-filesystem-direct.php |
Deletes a file or directory. |
WP_Filesystem_Direct::chgrp()wp-admin/includes/class-wp-filesystem-direct.php |
Changes the file group. |
WP_Filesystem_Direct::chmod()wp-admin/includes/class-wp-filesystem-direct.php |
Changes filesystem permissions. |
delete_plugins()wp-admin/includes/plugin.php |
Removes directory and files of a plugin for a list of plugins. |
update_core()wp-admin/includes/update-core.php |
Upgrades the core of WordPress. |
WP_Filesystem_ftpsockets::dirlist()wp-admin/includes/class-wp-filesystem-ftpsockets.php |
Gets details for files in a directory or a specific file. |
WP_Filesystem_ftpsockets::cwd()wp-admin/includes/class-wp-filesystem-ftpsockets.php |
Gets the current working directory. |
get_filesystem_method()wp-admin/includes/file.php |
Determines which method to use for reading, writing, modifying, or deleting files on the filesystem. |
get_home_path()wp-admin/includes/file.php |
Gets the absolute filesystem path to the root of the WordPress installation. |
list_files()wp-admin/includes/file.php |
Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep. |
unzip_file()wp-admin/includes/file.php |
Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction. |
copy_dir()wp-admin/includes/file.php |
Copies a directory from one location to another via the WordPress Filesystem Abstraction. |
paginate_links()wp-includes/general-template.php |
Retrieves paginated links for archive post pages. |
WP_Theme::scandir()wp-includes/class-wp-theme.php |
Scans a directory for files of a certain extension. |
wp_old_slug_redirect()wp-includes/query.php |
Redirect old slugs to the correct permalink. |
wp_unique_filename()wp-includes/functions.php |
Gets a filename that is sanitized and unique for the given directory. |
get_temp_dir()wp-includes/functions.php |
Determines a writable directory for temporary files. |
get_comments_pagenum_link()wp-includes/link-template.php |
Retrieves the comments page number link. |
paginate_comments_links()wp-includes/link-template.php |
Displays or retrieves pagination links for the comments on the current post. |
get_pagenum_link()wp-includes/link-template.php |
Retrieves the link for a page number. |
get_post_type_archive_feed_link()wp-includes/link-template.php |
Retrieves the permalink for a post type archive feed. |
get_term_feed_link()wp-includes/link-template.php |
Retrieves the feed link for a term. |
get_search_feed_link()wp-includes/link-template.php |
Retrieves the permalink for the search results feed. |
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_attachment_link()wp-includes/link-template.php |
Retrieves the permalink for an attachment. |
user_trailingslashit()wp-includes/link-template.php |
Retrieves a trailing-slashed string if the site is set for adding trailing slashes. |
WP_Admin_Bar::initialize()wp-includes/class-wp-admin-bar.php |
Initializes the admin bar. |
WP_Image_Editor::get_output_format()wp-includes/class-wp-image-editor.php |
Returns preferred mime-type and extension based on provided file’s extension and mime, or current file’s extension and mime. |
WP_Image_Editor::generate_filename()wp-includes/class-wp-image-editor.php |
Builds an output filename based on current file, and adding proper suffix |
plugin_dir_path()wp-includes/plugin.php |
Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in. |
plugin_dir_url()wp-includes/plugin.php |
Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in. |
_wp_link_page()wp-includes/post-template.php |
Helper function for wp_link_pages() . |
wp_get_attachment_url()wp-includes/post.php |
Retrieves the URL for an attachment. |
WP_Rewrite::mod_rewrite_rules()wp-includes/class-wp-rewrite.php |
Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess. |
redirect_guess_404_permalink()wp-includes/canonical.php |
Attempts to guess the correct URL for a 404 request based on query vars. |
url_to_postid()wp-includes/rewrite.php |
Examines a URL and try to determine the post ID it represents. |
redirect_canonical()wp-includes/canonical.php |
Redirects incoming links to the proper URL based on the site url. |
domain_exists()wp-includes/ms-functions.php |
Checks whether a site name is already taken. |
get_trackback_url()wp-includes/comment-template.php |
Retrieves the current post’s trackback URL. |
comments_template()wp-includes/comment-template.php |
Loads the comment template specified in $file. |
get_comment_link()wp-includes/comment-template.php |
Retrieves the link to a given comment. |
weblog_ping()wp-includes/comment.php |
Sends a pingback. |
_WP_Editors::editor_settings()wp-includes/class-wp-editor.php |
Changelog
| Version | Description |
|---|---|
| 1.2.0 | Introduced. |
Skip to note 3 content
Codex
Basic Example
$path will now contain:
/home/julien/bin/dotfiles/(Notice the trailing slash)
Skip to note 4 content
Brad Davis
Two examples that you could use the trailingslashit() function for:
To enqueue a style.css file
wp_enqueue_style( 'main-css', trailingslashit( get_template_directory_uri() ) . 'style.css' );To include a php file using the require statement
require trailingslashit( get_template_directory() ) . 'inc/custom-theme-functions.php';