user_trailingslashit() WordPress Function

The user_trailingslashit() function is a utility function that adds a trailing slash to a string, if it doesn't already have one. This is useful for ensuring that a string always has a trailing slash, for example when building paths to files or directories.

user_trailingslashit( string $string, string $type_of_url = '' ) #

Retrieves a trailing-slashed string if the site is set for adding trailing slashes.


Description

Conditionally adds a trailing slash if the permalink structure has a trailing slash, strips the trailing slash if not. The string is passed through the ‘user_trailingslashit’ filter. Will remove trailing slash from string, if site is not set to have them.


Top ↑

Parameters

$string

(string)(Required)URL with or without a trailing slash.

$type_of_url

(string)(Optional) The type of URL being considered (e.g. single, category, etc) for use in the filter.

Default value: ''


Top ↑

Return

(string) The URL with the trailing slash appended or stripped.


Top ↑

Source

File: wp-includes/link-template.php

function user_trailingslashit( $string, $type_of_url = '' ) {
	global $wp_rewrite;
	if ( $wp_rewrite->use_trailing_slashes ) {
		$string = trailingslashit( $string );
	} else {
		$string = untrailingslashit( $string );
	}

	/**
	 * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
	 *
	 * @since 2.2.0
	 *
	 * @param string $string      URL with or without a trailing slash.
	 * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
	 *                            'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',
	 *                            'category', 'page', 'year', 'month', 'day', 'post_type_archive'.
	 */
	return apply_filters( 'user_trailingslashit', $string, $type_of_url );
}


Top ↑

Changelog

Changelog
VersionDescription
2.2.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More