Last week, I wrote a post discussing why I think it’s important to use a trailing slash in WordPress.
The point of the post was to state that when you’re working with URLs and you’re looking for the last index of the array, the only way to guarantee the it’s always at the final position is if there’s a trailing slash so that you can `explode` on the ‘/’ character.
This post lead to some good discussion about why or why not to use `trailingslashit`, it brought to light to some other important and useful functions, and actually reminded me that there are times when trailing slashes should be used and when they shouldn’t.
To that end, I thought I’d thought I’d do a more thorough explanation as to each of the “trailing slash” functions WordPress offers, their purpose, and why trailing slashes are even important in the context of URLs.
The Trailing Slash in WordPress
As I mentioned earlier, using the trailing slash in WordPress is something that can provide benefits especially when it comes to locating the last index of the array.
The thing is, there are actually three different functions (at least, that I’m awareof) that deal with trailing slashes. On top of that, there’s a time and a place when when should include a trailing slash and when we shouldn’t.
When should we use a trailing slash in WordPress?
This is actually a question that can be more generalized that just WordPress. In fact, you could simply ask: “when should be use a trailing slash.”
The short answer is this:
- If your anchor is linking to a directory, then use a trailing slash.
- If it’s linking to a file, do not use a trailing slash.
Easy enough, right?
To that end, there’s actually more reading on this if you’re interested in the technical reasons for this. First, the Google Webmasters blog provides a good, short article on the topic. Secondly, the Wikipedia article on URI’s – or uniform resource identifiers – provides a much more technical piece on URL structures.
How do we remove a trailing slash in WordPress?
Before answering this question, I want to give Antonio props for bringing up this alternative scenario in the comments of the previous post.
In short, if you’re looking to guarantee that you will not have a trailing slash on the URL using the WordPress API, then take advantage of the `untrailingslashit` function.
Just as the Codex mentions:
Removes trailing slash if it exists.
But that’s not all. There’s a short note that the Codex also states that should be recognized:
The primary use of this is for paths and thus should be used for paths. It is not restricted to paths and offers no specific path support.
This means that if you’re trying to follow the best practices of using a slash for directories and no slashes for files, then using this function could have unintended consequences. Namely, it will remove the trailing slash from all URL’s that it’s given.
And how do we add a trailing slash in WordPress?
I covered this last week, but in order to be complete, I thought it may be worth summarizing again here in this post.
Simply put, if you’re looking to guarantee that you do have a slash at the end of the URL, then checkout the `trailingslashit` function in the WordPress API.
Practically speaking, this is the inverse of `untrailingslashit`. First, just as the Codex mentions:
Appends a trailing slash.
It also provides the same note that it’s inverse function offers:
The primary use of this is for paths and thus should be used for paths. It is not restricted to paths and offers no specific path support.
For those of you who are curious, `trailingslashit` actually uses `untrailingslashit` to remove any trailing slashes before adding a new one.
What is user_trailingslashit?
`user_trailingslashit` is an API function that WordPress provides that makes sure that the format of a given URL matches the settings that the user has configured in their settings.
At the time of this writing, I could not find a Codex article about this particular function; however, the code comments offer the following:
/** * Retrieve trailing slash string, if blog set for adding trailing slashes. * * 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 * blog is not set to have them. * * @since 2.2.0 * @uses $wp_rewrite * * @param string $string URL with or without a trailing slash. * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter. * @return string */
I’ve personally had to use this function when working on Standard and documented the reasons why in this post.
This particular function, although useful, is one that requires a bit of care as you need to really understand the path to which you’re linking and whether or not it should have a trailing slash or not.
Once you’ve determined that, prepare the URL then send it to this function. That should yield the best results.
A Summary of The Trailing Slash in WordPress
Dealing with trailing slashes isn’t so much complicated as it is knowing what you’re linking to and what the best practices for each scenario are.
Once you understand that, I think understanding exactly what functions to use makes sense – in fact, each one lends itself to the proper scenario:
- If you’re linking to a file, use `untrailingslashit`.
- If you’re linking to a path, use `trailingslashit`.
- If you’re looking to leverage the user’s settings, use `user_trailingslashit` but only after one of the above functions has processed the URL.
Hopefully, this post clarifies some issues around trailing slashes in WordPress, how to use, how to abuse, and how to mitigate whatever other crazy situations we end find ourselves in when working on projects.
Leave a Reply
You must be logged in to post a comment.