user_trailingslashit()WP 2.2.0

Adds or removes a trailing slash in the passed URL. It depends on the permalink structure.

Adds a slash to the end of the passed URL if the site post permalink structure has a slash at the end. And removes the trailing slash if permalink structure doesn't have a trailing slash.

The result is passed through the user_trailingslashit filter.

1 time — 0.00005 sec (very fast) | 50000 times — 0.076 sec (speed of light)
Hooks from the function

Return

String. The URL with the trailing slash appended or stripped.

Usage

user_trailingslashit( $string, $type_of_url );
$string(string) (required)
URL with or without a trailing slash to be processed.
$type_of_url(string)

The type of URL that is being considered. This parameter is passed to the user_trailingslashit hook for possibility to detect the type of link while the hook use. Known types:

  • single
  • single_trackback
  • single_feed
  • single_paged
  • feed
  • category
  • page
  • year
  • month
  • day
  • paged
  • post_type_archive

Default: ''

Examples

0

#1 How to delete or add a slash

Suppose we have http://example.com/foo/ URL. Let's see how the function will work depending on the current permalink structure of the website.

$url = 'http://example.com/foo/';

// Permalink structure: /%postname%
$url = user_trailingslashit( $url ); // Result: http://example.com/foo

// Permalink structure: /%postname%/
$url = user_trailingslashit( $url ); // Result: http://example.com/foo/

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 2.2.0 Introduced.

user_trailingslashit() code WP 6.5.2

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

	/**
	 * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes.
	 *
	 * @since 2.2.0
	 *
	 * @param string $url         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', $url, $type_of_url );
}