Home WordPress Links Functions user_trailingslashit() WordPress user_trailingslashit() Function 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.
<?php echo user_trailingslashit( 'https://example.com/about' ); ?> Call What 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 user_trailingslashit( string $url, string $type_of_url = '' ): string Parameters Parameter Description $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 Value Description string The 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.
1. Permalink structure ends with a slash 2. Permalink structure has no slash 3. Build a custom link Example 1 — Permalink structure ends with a slash Suppose your structure is /%postname%/.
<?php
echo user_trailingslashit( 'https://example.com/about' ); https://example.com/about/ How It Works Your permalinks end with a slash. So user_trailingslashit() adds one. 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.
<?php
echo user_trailingslashit( 'https://example.com/about/' ); https://example.com/about How It Works Your permalinks do not end with a slash. So the function removes the slash. 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.
<?php
function ctf_portfolio_url( $slug ) {
return user_trailingslashit( home_url( 'portfolio/' . $slug ) );
}
echo esc_url( ctf_portfolio_url( 'web-design' ) ); https://example.com/portfolio/web-design/ How It Works home_url() builds the full address.user_trailingslashit() fixes the ending to match your setting.The result is printed with esc_url(). Frequently Asked Questions What does user_trailingslashit() do? It adds or removes a trailing slash on a URL, so it matches your permalink setting.
What is the difference from trailingslashit()? trailingslashit() always adds a slash. user_trailingslashit() follows your permalink setting.
Where is the setting? In Settings, then Permalinks. If the structure ends with a slash, URLs get one.
What is the second argument for? It tells WordPress the type of URL, such as single or feed. Filters can use it. Most code leaves it empty.
Do I need it for every URL? Use it for URLs that you build yourself. Functions such as get_permalink() already do this for you.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion