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

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

Description 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 Parameters


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


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

Default value: ''

Top ↑

Return Return

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

Top ↑

Source 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

Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content

    you can remove category from wordpress url. See example below.

    function wpdocs_remove_category( $string, $type ) {
    	if ( 'single' !== $type && 'category' === $type && false !== strpos( $string, 'category' ) ) {
    		$url_without_category = str_replace( '/category/', '/', $string );
    		return trailingslashit( $url_without_category );
    	return $string;
    add_filter( 'user_trailingslashit', 'wpdocs_remove_category', 100, 2 );

You must log in before being able to contribute a note or feedback.