trailingslashit( string $value ): string
- Since
- 1.2.0
- Source
wp-includes/formatting.php:2813
Ensures a string ends with exactly one forward slash, stripping any trailing forward slashes or backslashes first so you never get a doubled separator. It is commonly used when concatenating a base path or URL with additional path segments. It does not validate that the input is an actual file path or URL, so it will happily add a slash to any string you pass it. For removing a trailing slash instead, use untrailingslashit.
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.
Compatibility
- WordPress
- since 1.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$valuestring- Value to which trailing slash will be added.
Return value
string- String with trailing slash added.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Build a safe plugin includes path for require statements
Some developers hardcode a plugin subdirectory without a trailing slash, which breaks when concatenated with a filename.
$plugin_dir = WP_PLUGIN_DIR . '/my-plugin';
$plugin_dir_slash = WP_PLUGIN_DIR . '/my-plugin/';
$normalized_one = trailingslashit( $plugin_dir );
$normalized_two = trailingslashit( $plugin_dir_slash );
echo esc_html( $normalized_one . 'includes/functions.php' ) . "\n";
echo esc_html( $normalized_two . 'includes/functions.php' );Both inputs produce the same result whether or not they already ended in a slash.
Append a path segment to a post permalink without double slashing
Permalinks returned by get_permalink() can differ in whether they already end with a slash, depending on the site's permalink settings.
$post_url = get_permalink( 1 );
$feed_url = trailingslashit( $post_url ) . 'feed/';
echo esc_html( $feed_url );Common problems and fixes · 3
- Why does trailingslashit leave backslashes in the middle of my Windows path untouched?
- Why did I end up with a double slash even after using trailingslashit?
- Does trailingslashit check that my string is actually a valid path or URL?
Why does trailingslashit leave backslashes in the middle of my Windows path untouched?
Why did I end up with a double slash even after using trailingslashit?
Does trailingslashit check that my string is actually a valid path or URL?
Alternatives and related functions
untrailingslashit- When you need to remove a trailing slash from a string rather than add one.
user_trailingslashit- When you are building a permalink and want to respect the site's configured permalink structure instead of always forcing a slash.
wp_normalize_path- When you need every backslash in a filesystem path converted to a forward slash, not just the trailing one.
Performance profile
How much work a call to trailingslashit() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 6.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost trailingslashit() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | untrailingslashit() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 6 instructions, 6 executed per call, 0 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
Used by · 50
- Core_Upgrader::upgrade()Upgrades WordPress core.
- Plugin_Upgrader::check_package()Checks that the source package contains a valid plugin.
- Plugin_Upgrader::delete_old_plugin()Deletes the old plugin during an upgrade.
- Theme_Upgrader::check_package()Checks that the package source contains a valid theme.
- Theme_Upgrader::delete_old_theme()Deletes the old theme during an upgrade.
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Block_Metadata_Registry::get_default_collection_roots()Gets the default collection root directory paths.
- WP_Filesystem_Base::find_folder()Locates a folder on the remote filesystem.
- WP_Filesystem_Base::search_for_folder()Locates a folder on the remote filesystem.
- WP_Filesystem_Direct::chgrp()Changes the file group.
- WP_Filesystem_Direct::chmod()Changes filesystem permissions.
- WP_Filesystem_Direct::delete()Deletes a file or directory.
Show all 50
- WP_Filesystem_Direct::dirlist()Gets details for files in a directory or a specific file.
- WP_Filesystem_FTPext::cwd()Gets the current working directory.
- WP_Filesystem_FTPext::delete()Deletes a file or directory.
- WP_Filesystem_FTPext::dirlist()Gets details for files in a directory or a specific file.
- WP_Filesystem_FTPext::is_dir()Checks if resource is a directory.
- WP_Filesystem_SSH2::cwd()Gets the current working directory.
- WP_Filesystem_SSH2::dirlist()Gets details for files in a directory or a specific file.
- WP_Filesystem_ftpsockets::cwd()Gets the current working directory.
- WP_Filesystem_ftpsockets::dirlist()Gets details for files in a directory or a specific file.
- WP_Image_Editor::generate_filename()Builds an output filename based on current file, and adding proper suffix
- WP_Image_Editor::get_output_format()Returns preferred mime-type and extension based on provided file's extension and mime, or current file's extension and mime.
- WP_REST_Global_Styles_Controller::prepare_links()Prepares links for the request.
- WP_REST_Menu_Locations_Controller::prepare_links()Prepares links for the request.
- WP_REST_Server::match_request_to_handler()Matches a request object to its handler.
- WP_Rewrite::mod_rewrite_rules()Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess.
- WP_Site_Health::wp_cron_scheduled_check()Runs the scheduled event to check and update the latest site health status for the website.
- WP_Theme::get_block_patterns()Gets block pattern data for a specified theme.
- WP_Theme::scandir()Scans a directory for files of a certain extension.
- WP_URL_Pattern_Prefixer::__construct()Constructor.
- WP_URL_Pattern_Prefixer::get_default_contexts()Returns the default contexts used by the class.
- WP_Upgrader::install_package()Install a package.
- WP_Upgrader::move_to_temp_backup_dir()Moves the plugin or theme being updated into a temporary backup directory.
- WP_Upgrader::restore_temp_backup()Restores the plugin or theme from temporary backup.
- _WP_Editors::editor_settings()
- _load_textdomain_just_in_time()Loads plugin and theme text domains just-in-time.
- _upgrade_422_find_genericons_files_in_folder()Recursively find Genericons example files in a given folder.
- _upgrade_422_remove_genericons()Cleans up Genericons example files.
- _wp_link_page()Helper function for wp_link_pages().
- _wp_upload_dir()A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
- comments_template()Loads the comment template specified in $file.
- copy_dir()Copies a directory from one location to another via the WordPress Filesystem Abstraction.
- delete_plugins()Removes directory and files of a plugin for a list of plugins.
- delete_theme()Removes a theme.
- domain_exists()Checks whether a site name is already taken.
- get_attachment_link()Retrieves the permalink for an attachment.
- get_author_feed_link()Retrieves the feed link for a given author.
- get_block_asset_url()Gets the URL to a block asset.
- get_comment_link()Retrieves the link to a given comment.
Source code
function trailingslashit( $value ) { return untrailingslashit( $value ) . '/';}Changelog
Introduced in 1.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.