untrailingslashit( string $value ): string
- Since
- 2.2.0
- Source
wp-includes/formatting.php:2839
Strips every trailing forward slash and backslash from a string, no matter how many are stacked at the end. Useful when concatenating a base path or URL with a second segment that already starts with a slash, since leftover trailing slashes would otherwise create a doubled separator. Use trailingslashit() when you want the opposite guarantee, exactly one trailing slash.
Description
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 2.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 from which trailing slashes will be removed.
Return value
string- String without the trailing slashes.
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 URL from home_url() without a doubled slash
Strip any trailing slash from the site URL before appending a path segment that starts with its own slash.
$base = home_url( '/blog/' );
$clean = untrailingslashit( $base );
$news_url = $clean . '/category/news';
echo esc_html( $news_url );Normalize the uploads base directory before appending a filename
wp_upload_dir() can return a basedir with or without a trailing separator depending on configuration, so normalize it first.
$upload = wp_upload_dir();
$dir = untrailingslashit( $upload['basedir'] );
$target_path = $dir . '/reports/summary.txt';
echo esc_html( $target_path );Common problems and fixes · 4
- Why did calling untrailingslashit on my URL not add a slash to the end?
- Why do I still get a double slash after using this function?
- Does it strip backslashes too, or only forward slashes?
- What happens if I pass an empty string or null?
Why did calling untrailingslashit on my URL not add a slash to the end?
Why do I still get a double slash after using this function?
Does it strip backslashes too, or only forward slashes?
What happens if I pass an empty string or null?
Alternatives and related functions
trailingslashit- When you need the string to end with exactly one trailing slash instead of none.
wp_normalize_path- When you're working with a filesystem path and need consistent forward slashes across operating systems, not just trailing-slash removal.
rtrim- When you need to trim a custom set of trailing characters that isn't just slashes and backslashes.
path_join- When you need to safely join two path segments together rather than manually trimming and concatenating them.
Performance profile
How much work a call to untrailingslashit() 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
- 47
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.
47 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 untrailingslashit() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | rtrim() |
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.
Used by · 47
- WP_Automatic_Updater::is_vcs_checkout()Checks for version control checkouts.
- WP_Comments_List_Table::column_author()Outputs the author column.
- WP_Debug_Data::get_sizes()Fetches the sizes of the WordPress directories: `wordpress` (ABSPATH), `plugins`, `themes`, and `uploads`.
- WP_Debug_Data::get_wp_paths_sizes()Gets the WordPress paths and sizes section of the debug data.
- WP_Filesystem_Base::search_for_folder()Locates a folder on the remote filesystem.
- WP_Filesystem_Direct::mkdir()Creates a directory.
- WP_Filesystem_FTPext::mkdir()Creates a directory.
- WP_Filesystem_SSH2::mkdir()Creates a directory.
- WP_Filesystem_ftpsockets::mkdir()Creates a directory.
- WP_MS_Sites_List_Table::column_blogname()Handles the site name column output.
- WP_MS_Sites_List_Table::column_cb()Handles the checkbox column output.
- WP_MS_Sites_List_Table::handle_row_actions()Generates and displays row action links.
Show all 47
- WP_REST_Request::from_url()Retrieves a WP_REST_Request object from a full URL.
- WP_REST_URL_Details_Controller::parse_url_details()Retrieves the contents of the title tag from the HTML response.
- WP_Widget_RSS::widget()Outputs the content for the current RSS widget instance.
- __get_option()Utility version of get_option that is private to installation/upgrade.
- _config_wp_home()Retrieves the WordPress home page URL.
- _config_wp_siteurl()Retrieves the WordPress site URL.
- _load_script_textdomain_from_src()Resolves and loads the translation JSON file for a given script or script module source URL.
- _unzip_file_pclzip()Attempts to unzip an archive using the PclZip library.
- _unzip_file_ziparchive()Attempts to unzip an archive using the ZipArchive class.
- _wp_before_delete_font_face()Deletes associated font files when a font face is deleted.
- _wp_filter_font_directory()A callback function for use in the {@see 'upload_dir'} filter.
- _wp_menu_item_classes_by_context()Adds the class property classes for the current context, if applicable.
- _wp_upload_dir()A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
- clean_dirsize_cache()Cleans directory size cache used by recurse_dirsize().
- confirm_another_blog_signup()Shows a message confirming that the new site has been created.
- get_alloptions_110()Retrieve all options as it was for 1.2.
- get_option()Retrieves an option value based on an option name.
- get_pagenum_link()Retrieves the link for a page number.
- get_sample_permalink()Returns a sample permalink based on the post name.
- install_blog()Install an empty blog.
- paginate_links()Retrieves paginated links for archive post pages.
- recurse_dirsize()Gets the size of a directory recursively.
- redirect_canonical()Redirects incoming links to the proper URL based on the site url.
- register_theme_directory()Registers a directory that contains themes.
- render_block_core_breadcrumbs()Renders the `core/breadcrumbs` block on the server.
- render_block_core_rss()Renders the `core/rss` block on server.
- rest_api_loaded()Loads the REST API.
- rest_preload_api_request()Append result of internal request to REST API for purpose of preloading data to be attached to a page.
- trailingslashit()Appends a trailing slash.
- unzip_file()Unzips a specified ZIP file to a location on the filesystem via the WordPress Filesystem Abstraction.
- url_shorten()Shortens a URL, to be used as link text.
- user_trailingslashit()Retrieves a trailing-slashed string if the site is set for adding trailing slashes.
- wp_initialize_site()Runs the initialization routine for a given site.
- wp_redirect_admin_locations()Redirects a variety of shorthand URLs to the admin.
- wp_update_https_migration_required()Updates the 'https_migration_required' option if needed when the given URL has been updated from HTTP to HTTPS.
Source code
function untrailingslashit( $value ) { return rtrim( $value, '/\\' );}Changelog
Introduced in 2.2.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$value retyped from untyped to string.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.