Remove “category” from URLs

WordPress, by default, adds the word “category” into your category links, and the word “tag” into your tag links. The workaround used to be to add a period into the base settings of each one (seen in the screenshot above). If you make these changes, the first page of your category will work, but the second page will cause errors. To properly set this up, I’ve included code below to make this customization. In your theme’s functions.php file, copy and paste all this code (rename “sharon” to whatever you wish to make it your own):

<?php
/**
 * Remove "category" from URLs
 * last updated: 01-09-2024
 * Author: Sharon Murphy
**/

function sharon_category_flush_rules() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}
add_action( 'created_category', 'sharon_category_flush_rules' );
add_action( 'delete_category', 'sharon_category_flush_rules' );
add_action( 'edited_category', 'sharon_category_flush_rules' );

function rass_category_init() {
	global $wp_rewrite, $wp_version;
	if ( 3.4 <= $wp_version ) {
		$wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
	} else {
		$wp_rewrite->extra_permastructs['category'][0] = '%category%';
	}
}
add_action( 'init', 'rass_category_init' );

function sharon_category_rewrite_rules( $category_rewrite ) {
	global $wp_rewrite;
	$category_rewrite = array();
	$categories = get_categories( array( 'hide_empty' => false ) );
	foreach ( $categories as $category ) {
		$category_nicename = $category->slug;
		if ( $category->parent == $category->cat_ID ) {
			$category->parent = 0;
		} elseif ( 0 != $category->parent ) {
			$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
		}
		$category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
		$category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
		$category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]';
	}
	$old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category';
	$old_category_base = trim( $old_category_base, '/' );
	$category_rewrite[ $old_category_base . '/(.*)$' ] = 'index.php?category_redirect=$matches[1]';
	return $category_rewrite;
}
add_filter( 'category_rewrite_rules', 'sharon_category_rewrite_rules' );

function sharon_category_query_vars( $public_query_vars ) {
	$public_query_vars[] = 'category_redirect';
	return $public_query_vars;
}
add_filter( 'query_vars', 'sharon_category_query_vars' );

function sharon_category_request( $query_vars ) {
	if ( isset( $query_vars['category_redirect'] ) ) {
		$catlink = trailingslashit( home_url() ) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
		status_header( 301 );
		header( "Location: $catlink" );
		exit;
	}
	return $query_vars;
}
add_filter( 'request', 'sharon_category_request' );

Leave a Reply

Your email address will not be published. Required fields are marked *