wppaste
WordPress

trailingslashit( string $value ): string

Since
1.2.0
Source
wp-includes/formatting.php:2803

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.

Appends a trailing slash.

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?

The function only strips slashes or backslashes at the very end of the string (via untrailingslashit) before appending one forward slash. It never touches separators elsewhere in the string. - Use wp_normalize_path() first if you need every backslash converted to a forward slash throughout a filesystem path.

Why did I end up with a double slash even after using trailingslashit?

trailingslashit only cleans up the string you pass in. If the next piece you concatenate onto the result also starts with a slash, you get two slashes where they meet. - Check the segment you append and strip its leading slash, or use ltrim( $segment, '/' ) before concatenating.

Does trailingslashit check that my string is actually a valid path or URL?

No. Per its own documentation it offers no specific path support and works on any string, so it will add a trailing slash to arbitrary text just as readily as to a real path. - Validate or sanitize the value separately (for example with esc_url() for URLs or realpath() for filesystem paths) before or after calling it.

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

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
50

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.

WhenInstructionsCalls it makes
always6untrailingslashit()

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

Used by · 50

Show all 50

Source code

function trailingslashit( $value ) {	return untrailingslashit( $value ) . '/';}

Changelog

Introduced in 1.2.0. Unchanged from 6.7.7 through 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.

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.