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( $string );
$string(string) (required)
What to add the trailing slash to.

Examples

0

#1 Demo:

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

echo trailingslashit( "any (string)" ) ); //> any (string)/
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.1.1

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