untrailingslashit( $value ): string
- Since
- 2.2.0
- Source
wp-includes/formatting.php:2818
Strips one or more trailing `/` and `\` characters from the end of a string using rtrim(), most often to normalize a path or URL before concatenation. It does not validate that the input is actually a path or URL, and it will strip every matching character at the end, not just one. Pair it with trailingslashit() when you need the opposite guarantee, and with wp_normalize_path() if you also need consistent slash direction.
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
$value
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.
Remove a trailing slash from a theme directory path before appending a filename
Concatenating a directory constant with a filename often produces an accidental double slash unless the directory is normalized first.
$theme_dir = get_template_directory() . '///';
$clean_dir = untrailingslashit( $theme_dir );
$screenshot_path = $clean_dir . '/screenshot.png';
echo esc_html( $screenshot_path );get_template_directory() never actually ends in slashes; the extra slashes here are added just to show untrailingslashit() removing them.
Clean up an API endpoint that mixes trailing slashes and backslashes
A base URL pulled from settings or a config array can end in a stray slash, backslash, or a run of both.
$raw_endpoint = 'https://api.example.com/v2/reports\\/';
$clean_endpoint = untrailingslashit( $raw_endpoint );
echo esc_html( $clean_endpoint . '?post=1' );Common problems and fixes · 4
- Why does untrailingslashit() strip more than one trailing slash?
- Why did untrailingslashit() return an empty string?
- Why doesn't untrailingslashit() add a slash back for me?
- Can I pass a non-string value like null or an array to untrailingslashit()?
Why does untrailingslashit() strip more than one trailing slash?
Why did untrailingslashit() return an empty string?
Why doesn't untrailingslashit() add a slash back for me?
Can I pass a non-string value like null or an array to untrailingslashit()?
Alternatives and related functions
trailingslashit- When you need the string to end in exactly one trailing slash instead of having trailing slashes removed.
wp_normalize_path- When you also need backslashes converted to forward slashes and duplicate slashes collapsed, not just trailing slashes removed.
path_join- When you are combining a base directory and a relative path and want WordPress to handle the join safely instead of concatenating strings yourself.
rtrim- When you need to trim a custom set of trailing characters that isn't just slashes and backslashes.
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
- 45
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.
45 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 · 45
- WP_Automatic_Updater::is_vcs_checkout()Checks for version control checkouts.
- WP_Comments_List_Table::column_author()
- 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 45
- 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.
- _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.
- load_script_textdomain()Loads the script translated strings.
- 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_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 6.7.7 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.