From dcf1342be90fc61bb8bb3030f92cf7e7e0dbeff6 Mon Sep 17 00:00:00 2001 From: Chuck Reynolds Date: Mon, 9 Jun 2025 18:06:59 -0700 Subject: [PATCH 1/2] Update cat base to handle redirects properly Updated to handle redirects properly. It was doing /blog/category/foocat/ => /foocat and 404'ing... This patch should handle all/most? (test) redirects accordingly; example /blog/category/foocat/ => /blog/foocat/ --- permalinks/tsf-remove-category-base.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/permalinks/tsf-remove-category-base.php b/permalinks/tsf-remove-category-base.php index 624b55b..2861a39 100644 --- a/permalinks/tsf-remove-category-base.php +++ b/permalinks/tsf-remove-category-base.php @@ -3,7 +3,7 @@ * Plugin Name: The SEO Framework - Remove category base * Plugin URI: https://theseoframework.com/ * Description: Removed the category base from the URL, akin to how Yoast SEO does it. - * Version: 1.0.0 + * Version: 1.0.1 * Author: Sybre Waaijer * Author URI: https://theseoframework.com/ * License: GPLv3 @@ -101,10 +101,16 @@ function redirect_base( $query_vars ) { if ( empty( $query_vars['mytsf_category_redirect'] ) ) return $query_vars; - \wp_safe_redirect( - \trailingslashit( \get_option( 'home' ) ) . \user_trailingslashit( $query_vars['mytsf_category_redirect'], 'category' ), - 301, - ); + // Determine blog prefix, similar to modify_category_rewrite_rules + $permalink_structure = \get_option( 'permalink_structure' ); + $blog_prefix = \is_main_site() && str_starts_with( $permalink_structure, '/blog/' ) + ? 'blog/' + : ''; + + // Build the redirect URL + $redirect_url = \trailingslashit( \get_option( 'home' ) ) . $blog_prefix . \user_trailingslashit( $query_vars['mytsf_category_redirect'], 'category' ); + + \wp_safe_redirect( $redirect_url, 301 ); exit; } From 4f9664c260f25285cf5d02114ff86ad17bd8b3ef Mon Sep 17 00:00:00 2001 From: Chuck Reynolds Date: Wed, 11 Jun 2025 14:05:20 -0700 Subject: [PATCH 2/2] Update redirect_base() in tsf-remove-category-base.php Use get_category_link() to get the correct URL for the category by slug. Use the slug from the redirect match to get the term object. Redirect to the canonical category URL using get_category_link() --- permalinks/tsf-remove-category-base.php | 34 +++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/permalinks/tsf-remove-category-base.php b/permalinks/tsf-remove-category-base.php index 2861a39..242b863 100644 --- a/permalinks/tsf-remove-category-base.php +++ b/permalinks/tsf-remove-category-base.php @@ -3,7 +3,7 @@ * Plugin Name: The SEO Framework - Remove category base * Plugin URI: https://theseoframework.com/ * Description: Removed the category base from the URL, akin to how Yoast SEO does it. - * Version: 1.0.1 + * Version: 1.0.2 * Author: Sybre Waaijer * Author URI: https://theseoframework.com/ * License: GPLv3 @@ -91,29 +91,37 @@ function register_query_vars( $query_vars ) { * Checks whether the redirect needs to be created. * * @hook request 10 - * @since 1.0.0 + * @since 1.0.2 * * @param array $query_vars Query vars to check for existence of redirect var. * @return array The query vars. */ function redirect_base( $query_vars ) { - - if ( empty( $query_vars['mytsf_category_redirect'] ) ) + if ( empty( $query_vars['mytsf_category_redirect'] ) ) { return $query_vars; + } - // Determine blog prefix, similar to modify_category_rewrite_rules - $permalink_structure = \get_option( 'permalink_structure' ); - $blog_prefix = \is_main_site() && str_starts_with( $permalink_structure, '/blog/' ) - ? 'blog/' - : ''; - - // Build the redirect URL - $redirect_url = \trailingslashit( \get_option( 'home' ) ) . $blog_prefix . \user_trailingslashit( $query_vars['mytsf_category_redirect'], 'category' ); + // Get the category by slug (the matched part) + $category = get_category_by_slug( $query_vars['mytsf_category_redirect'] ); + if ( $category && ! is_wp_error( $category ) ) { + $redirect_url = get_category_link( $category->term_id ); + wp_safe_redirect( $redirect_url, 301 ); + exit; + } - \wp_safe_redirect( $redirect_url, 301 ); + // Fallback: redirect to home if not found + wp_safe_redirect( home_url(), 301 ); exit; } +/** + * Helper: Get category by slug. + */ +function get_category_by_slug( $slug ) { + $term = get_term_by( 'slug', $slug, 'category' ); + return $term; +} + /** * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta. *