WordPress user_trailingslashit() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a URL

Definition

In WordPress, user_trailingslashit() adds or removes the slash at the end of a URL, based on your permalink setting. If your permalink structure ends with /, it adds one. If not, it removes it.

Use it when you build your own URLs, so they match the other links on your site. It is different from trailingslashit(), which always adds a slash no matter what the setting says.

Syntax

Pass the URL. It returns the URL with the right ending.

WordPress
<?php echo user_trailingslashit( 'https://example.com/about' ); ?>
CallWhat it does
user_trailingslashit( $url )Fix the ending of a URL.
user_trailingslashit( $url, 'single' )Same, and tell WordPress what kind of URL it is.
user_trailingslashit( home_url( 'portfolio' ) )Build a custom link with the right ending.

It returns the URL. It does not print anything.

Full declaration
WordPress
user_trailingslashit( string $url, string $type_of_url = '' ): string

Parameters

ParameterDescription
$urlstring. The URL to change. It is required.
$type_of_urlstring. The kind of URL, such as single, page, category, or feed. Most code leaves it empty. It lets filters treat some URL types in a special way.

Return Value

Return ValueDescription
stringThe URL with a trailing slash, or without one, depending on your permalink structure.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Permalink structure ends with a slash

Suppose your structure is /%postname%/.

WordPress
<?php
echo user_trailingslashit( 'https://example.com/about' );

How It Works

  1. Your permalinks end with a slash.
  2. So user_trailingslashit() adds one.
  3. The link now matches your other page links.

Example 2 — Permalink structure has no slash

Now suppose your structure is /%postname%, with no slash at the end.

WordPress
<?php
echo user_trailingslashit( 'https://example.com/about/' );

How It Works

  1. Your permalinks do not end with a slash.
  2. So the function removes the slash.
  3. Same code, different result, because the setting is different.

Example 3 — Build a custom link

Create a helper for a custom section of your site.

WordPress
<?php
function ctf_portfolio_url( $slug ) {
  return user_trailingslashit( home_url( 'portfolio/' . $slug ) );
}
echo esc_url( ctf_portfolio_url( 'web-design' ) );

How It Works

  1. home_url() builds the full address.
  2. user_trailingslashit() fixes the ending to match your setting.
  3. The result is printed with esc_url().

Frequently Asked Questions

It adds or removes a trailing slash on a URL, so it matches your permalink setting.
trailingslashit() always adds a slash. user_trailingslashit() follows your permalink setting.
In Settings, then Permalinks. If the structure ends with a slash, URLs get one.
It tells WordPress the type of URL, such as single or feed. Filters can use it. Most code leaves it empty.
Use it for URLs that you build yourself. Functions such as get_permalink() already do this for you.

Next: wp_shortlink_header()

Send a Link: shortlink HTTP header for the current page.

wp_shortlink_header() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful