user_trailingslashitfilter-hookWP 2.2.0

Allows you to remove/add a trailing slash (/) to URLs processed by user_trailingslashit().

Usage

add_filter( 'user_trailingslashit', 'wp_kama_user_trailingslashit_filter', 10, 2 );

/**
 * Function for `user_trailingslashit` filter-hook.
 * 
 * @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 string
 */
function wp_kama_user_trailingslashit_filter( $url, $type_of_url ){
	// filter...
	return $url;
}
$string(string)
The URL with or without a trailing slash (/).
$type_of_url(string)

The type of URL being processed. It can be:

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

Examples

#1 Remove the trailing slash from pages (posts of the page type)

Suppose a custom post type with a configured slug still has a trailing slash in its post URL, for example: example.com/post_pro_kenguru/.

The main permalink rule in the settings determines whether there is a trailing slash. If the structure ends with a slash, it is automatically added to all post types. This behavior can be changed through this hook:

This example shows how to remove the slash from posts of the page type. Any post type can be handled similarly:

// Remove the trailing slash if necessary. If the permalink structure has a trailing slash, it is also added to permanent pages.
add_filter( 'user_trailingslashit', 'no_page_slash', 70, 2 );

function no_page_slash( $string, $type ){
   global $wp_rewrite;

	if( 'page' === $type && $wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes )
		$string = untrailingslashit( $string );

   return $string;
}

For another post type, replace 'page' === $type with the name of that post type.

Changelog

Since 2.2.0 Introduced.

Where the hook is called

user_trailingslashit()
user_trailingslashit
wp-includes/link-template.php 65
return apply_filters( 'user_trailingslashit', $url, $type_of_url );

Where the hook is used in WordPress

Usage not found.