trailingslashit( string $value ): string

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.

Parameters

$valuestringrequired
Value to which trailing slash will be added.

Return

string String with trailing slash added.

Source

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

Changelog

VersionDescription
1.2.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Two examples that you could use the trailingslashit() function for:
    To enqueue a style.css file

    wp_enqueue_style( 'main-css', trailingslashit( get_template_directory_uri() ) . 'style.css' );

    To include a php file using the require statement

    require trailingslashit( get_template_directory() ) . 'inc/custom-theme-functions.php';
  2. Skip to note 6 content

    Handling Existing Trailing Slashes

    This example demonstrates that trailingslashit() will remove any existing trailing forward or backslashes before adding a single trailing forward slash, preventing double slashing.

    $path1 = trailingslashit( ‘/wp-content/themes/my-theme’ );
    $path2 = trailingslashit( ‘/wp-content/themes/my-theme/’ );

    All two will result in the same output:
    $path1 = /wp-content/themes/my-theme/
    $path2 = /wp-content/themes/my-theme/

You must log in before being able to contribute a note or feedback.