trailingslashit()WP-CLI 1.0

Adds a slash / at the end of the string.

Before adding /, the function removes all / at the end of the string. This prevents double slashes in file paths or links.

No Hooks.

Returns

String. A string with / added at the end.

Usage

$string = trailingslashit( $string );
$string(string) (required)
The string to which you need to add / at the end.

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';

trailingslashit() code WP-CLI 2.8.0-alpha

function trailingslashit( $string ) {
	if ( ! is_string( $string ) ) {
		return '/';
	}

	return rtrim( $string, '/\\' ) . '/';
}