wppaste
WordPress

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.

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

$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?

The function calls rtrim( $value, '/\\' ), and rtrim removes every trailing character that matches the given set, not just the last one. A string like 'foo///' becomes 'foo' in a single call, not 'foo//'.

Why did untrailingslashit() return an empty string?

If the entire input is made of slashes or backslashes, such as '///' or '\\\\', rtrim keeps trimming until nothing matches and returns ''. This is expected behavior of rtrim, not a bug in the function.

Why doesn't untrailingslashit() add a slash back for me?

This function only removes trailing slashes and backslashes; it never adds one. Reaching for it when you actually want a guaranteed trailing slash is a common mix-up with its counterpart.

Can I pass a non-string value like null or an array to untrailingslashit()?

The $value parameter has no type declaration, and it is passed straight to rtrim(). Passing null triggers a deprecation notice on PHP 8.1+, and passing an array or object triggers a fatal TypeError inside rtrim().

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

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.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.