Remove category and tag base from WordPress URLs without a plugin?
📝 How to Remove Category and Tag Base from WordPress URLs without a Plugin
Are you tired of seeing that pesky category and tag base in your WordPress URLs? 😫 Don't worry, we've got you covered! In this blog post, we'll provide you with easy solutions to remove the category and tag base from your WordPress URLs without relying on plugins. 💪
The Problem: You want to remove the category and tag base from your WordPress URLs, but you don't want to use a plugin. You want a solution that resides inside the functions.php file to avoid any conflicts with future plugin updates or changes to WordPress default files. We understand your concerns and want to help you achieve your desired URL structure! 🌐
The Failed Attempts: We understand that you've already tried a couple of solutions, but they didn't work out for you. You mentioned that the htaccess solution provided by Mike Payne and the methods suggested by AskApache didn't do the trick. It's time to explore alternative solutions! 🚀
The Easy Solution:
Open your WordPress theme's functions.php file. You can find it in your theme's directory.
Before making any changes, it's strongly recommended to create a backup of the file, just in case.
Now, let's dive into the code that will help you remove the category and tag base from your WordPress URLs:
/**
* Remove Category and Tag Base from WordPress URLs
*/
function remove_category_tag_base() {
global $wp_rewrite;
$wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
$wp_rewrite->extra_permastructs['post_tag']['struct'] = '%post_tag%';
}
add_action('init', 'remove_category_tag_base');
Once you've added this code snippet to your functions.php file, save the changes and refresh your WordPress site. 🔄
The Explanation:
Let's break down the code above so you can understand what's happening:
We're using the
remove_category_tag_base()
function to modify the WordPress rewrite rules and remove the category and tag bases from the URLs. 🔄By setting the
struct
property of theextra_permastructs
array for the category and post_tag taxonomies to their respective placeholders (%category%
and%post_tag%
), we effectively remove the bases from the URLs. 🔧The
add_action()
function is used to ensure that our custom function is hooked into theinit
action, allowing it to run at the appropriate time during WordPress initialization. 🕒
The Call-to-Action:
Now that you've successfully removed the category and tag base from your WordPress URLs, it's time to sit back and enjoy your cleaner and more user-friendly URLs! 🎉
If you found this guide helpful, please consider sharing it with others who might be facing the same issue. Spread the knowledge and empower your fellow WordPress enthusiasts! 👏
Have any questions, or want to share your experiences with removing the category and tag base from WordPress URLs? Drop a comment below, and let's start a conversation! 🗯️ We love hearing from our readers.
Happy URL structuring! 😎