wppaste
WordPress

untrailingslashit( string $value ): string

Since
2.2.0
Source
wp-includes/formatting.php:2836

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.

Removes trailing forward slashes and backslashes if they exist.

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?

untrailingslashit only removes trailing slashes, it never adds one. It's easy to reach for it when you actually want a guaranteed trailing slash. - Use trailingslashit() instead if you need the string to end with exactly one slash.

Why do I still get a double slash after using this function?

untrailingslashit only strips slashes from the end of the string you pass it. If the second string you concatenate onto the result already starts with '/', you get '//' at the join point regardless of what untrailingslashit did.
$result = untrailingslashit( $base ) . '/' . ltrim( $segment, '/' ); echo esc_html( $result );

Does it strip backslashes too, or only forward slashes?

The implementation calls rtrim( $value, '/\\' ), so both characters are in the trim mask and any mix of them at the end (////, \\\\, or a mixture) is removed in one pass, not just a single trailing slash.

What happens if I pass an empty string or null?

rtrim() on an empty string simply returns an empty string, so there's no error there. Passing null is the risk: as of PHP 8.1, passing null to a string parameter like rtrim()'s triggers a deprecation notice, so cast or check the value before calling untrailingslashit() on something that might be 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6

Executed per call on PHP 8.5. The body compiles to 6.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
45

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.

WhenInstructionsCalls it makes
always6rtrim()

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

Show all 45

Source code

function untrailingslashit( $value ) {	return rtrim( $value, '/\\' );}

Changelog

Introduced in 2.2.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.8.8
Parameter $value retyped from untyped to string.verified against source
2.2.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.