Strip / Remove Category Base From Rank Math Breadcrumbs!
Table of Contents
Introduction
For both user experience and search engine optimization (SEO), your website’s navigation needs to be enhanced. Breadcrumbs give users a hierarchical link trail, assisting them in navigating your website. A well-liked SEO plugin for WordPress named Rank Math has a number of functions, including breadcrumbs. According to the permalink structure of a WordPress site, the category base is included in the URLs by default.
Although breadcrumbs provide visitors with helpful navigation, some website owners decide to remove the category base from the breadcrumb URLs because it might not suit every website’s preferences. As many website owners prefer cleaner, simple, and more SEO-friendly URLs without the category base for better results in SEO. Many of them have unique URL configurations, and I can guess that, they also need this filter.
Anyway, we are going to explore utilizing a PHP filter / programmatically to accomplish this in this tutorial without wasting much time.
Note: the code removes the category base only from the breadcrumbs and will not take effect anywhere else.
Filter to Remove Category Base From Rank Math Breadcrumbs
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
$get_post_category = get_the_category();
if (!empty($post_categories)) {
foreach($get_post_category as $category) {
$url = get_category_link( $category->term_id );$i=0;
while(count($crumbs) >= $i){
if($url == $crumbs[$i][1]){
$crumbs[$i][1] = str_replace('/category/', '/', $url);
}$i++;
}
}
}
return $crumbs;
}, 10, 2);
Expected Output:
The URL for category crumbs will change from https://domain.com/category/your-slug
to https://domain.com/your-slug
in Rank Math breadcrumbs.
A Tiny Tweak For Custom Category Base
However, there isn’t much to be explained about the code. The code simply looks for the categories you have set up for your specific post and searches for them in your breadcrumbs as well. The code just removes the /category/ base from your breadcrumbs URL when it finds a match.
It’s important to keep in mind that the code needs a tiny tweak if your category base has been modified under WordPress Dashboard > Settings > Permalinks > Optional > Category Base.
Don’t get stressed, it’s quite simple! Simply change the base name with your own in the lines of code below:
$crumbs[$i][1] = str_replace('/YOUR CATEGORY BASE HERE/', '/', $url);
How to add filters or Hooks to your WordPress site
I know still some of us get confused about where and how we should add filters or hooks to our WordPress site. For Rank Math, I’d recommend the Knowledge base article that Rank Math published: How to Add a Filter/Hook to Your Website?
Conclusion
It’s simple to remove the category base from Rank Math breadcrumbs by adding a short bit of code to your website. Using this piece of code, you can change the Rank Math breadcrumbs according to the needs of your website. Make sure to create a backup of your website before making any modifications to your code. Additionally, if the modifications are not appearing on your site in some way, remember to clear the site cache.
Additionally, SEO is a continuous process, therefore it’s essential to frequently analyze and improve the overall structure and content of your website.
It’s important to remember that changing the breadcrumb URLs can have an impact on your site’s SEO and user experience. Before applying the modifications on a live site, make sure you thoroughly test them and keep an eye out for any possible effects on your website.
I hope that was helpful. Thanks for your support!
Categories: WordPress, Rank Math, WordPress SEO
Got any questions? Feel free to ask them in the comment section below.