When it comes to working with URLs, I believe that we should often be using `trailingslashit` in WordPress.
This is post is part of a larger discussion that I’ll be covering in more detail in another post, but here’s the gist of why I’m a fan of using `trailingslashit`.
Here’s why: there’s only a handful of operations that you can really be doing:
- Setting up some type of rewrite rules or routes
- Sniffing out parts of the URL to check for data
- Redirecting (or protecting) users from content based on their status
- …and maybe a few more
When it comes to examining the contents of the URL, one of the most common – if not the most common – way of doing it is using `explode` and then examine the various indexes of the array.
But in order to properly handle this, I think that we have an obligation to code defensively against URLs because we never know how users, third party libraries, or other clients are going to provide said URL.
And thus the case for always using `trailingslashit` in WordPress projects.
