trailingslashit()WP 1.2.0

Appends a trailing slash.

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.

No Hooks.

Return

String. String with trailing slash added.

Usage

trailingslashit( $value );
$value(string) (required)
Value to which trailing slash will be added.

Examples

0

#1 Demo:

echo trailingslashit( "any (string)//" ); //> any (string)/

echo trailingslashit( "any (string)" ) ); //> any (string)/

echo trailingslashit( trailingslashit( 'some/dir/' ) ); //> some/dir/
0

#2 Examples that you could use the this 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';

Changelog

Since 1.2.0 Introduced.

trailingslashit() code WP 6.4.3

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