Remove Trailing Slash in Pagination Link | WordPress in 1 Min

In this guide, I will show you how to remove trailing slash in pagination links on your WordPress theme. This is quick and easy if you have access to your theme code and are able to modify it.

By default WordPress adds a trailing slash on the pagination links as seen in the screen shot below. This is a fresh site using the WordPress “Twenty Twenty-Two” theme and some dummy data. I prefer doing examples like this with the least modified site / code possible to make it easier to follow the instructions and get a successful result.

What is a trailing slash?

A trailing slash is when a URL ends with a slash. For example, ends with a trailing slash at the end.

Next page with trailing slash

An example of not ending without a trailing slash for this same URL would be

Remove Trailing Slash in Pagination Link

Why remove trailing slash from the pagination link in WordPress pagination?

You might ask yourself, why would I want to remove the trailing slash from the pagination links and that is a very valid question. In my opinion, it all comes down to consistency of the site and your preference. When creating a site from scratch you can choose to end with a trailing slash or not.

By default, WordPress will always use the trailing slash for all of the dynamically generated links in the site. You can override this by changing the settings in permalinks. If you would like to learn more about this, drop a comment below and I can write an article on it.

Before you change the trailing slash

In my past experience, we converted a site written from scratch to a WordPress website to help allow the content team to manage and publish their own content. The previous site coming from a JavaScript-based website was written to not include the trailing slash. So when we made the migration we kept things consistent so that we would not have to worry about SEO impact or setting up redirects with a bunch of changed URLs. Make sure to consider what the impact of this will be on your SEO and website experience.

Remove trailing slash in pagination link in WordPress

To remove the trailing slash from the pagination in WordPress it is as simple as putting this one liner in you functions.php file.

Note: If the file does not exist, you should be able to create it in the root of your active theme directory.

// Remove trailing slash from pagination links
add_filter( 'get_pagenum_link', 'untrailingslashit');

We now see that the pagination link does not include a trailing slash! I told you it was going to be easy! So what just happened here? We can break it down as follows.

add_filter We use a WordPress hook that hooks into the internal data at runtime and allows us to make modifications before it is returned to the page. It takes two input parameters. 1) Is the hook that we are trying to tap into, and 2) The callback function we want to run when the hook is called.

We then run two sets of filters as seen in the code above, paginate_link and get_pagenum_link.

  • paginate_link It specifically allows us to modify paginated number links for the given archive pages.
  • get_pagenum_link It specifically allows us to change the next and previous links for the archive pagination.
wordpress remove trailing slash hooks

untrailingslashit We pass it as the callback function when the filter is called. untrailingslashit, which is a WordPress function, will remove a trailing backslash or forward slash if present.

Wrap up

As you can see it was easy to remove trailing slash in pagination link within WordPress. WordPress pagination is a very powerful feature and making sure that you have it set up correctly is very important to keep your site consistent. What is your preference for the trailing slash on your website? Have you modified your WordPress Pagination? Drop a comment below to get the conversation started.

If you would like to suggest a new article or video to be released, please reach out via the Contact Page. I look forward to hearing your feedback and providing content that is helpful for you!

Leave a Comment