WordPress at a glance

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.

Is the basis for: get_comments_pagenum_link()
✈ 1 time = 0.00005s = very fast | 50000 times = 0.076s = speed of light
Hooks in function

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


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

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


#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/

Code of user trailingslashit: wp-includes/link-template.php WP 5.2.2

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

Related Functions

From category: Formatting

No comments
    Hello, !     Log In . Register