Skip to content

Update cat base to handle redirects properly #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions permalinks/tsf-remove-category-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.2
* Author: Sybre Waaijer
* Author URI: https://theseoframework.com/
* License: GPLv3
Expand Down Expand Up @@ -91,23 +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<string> $query_vars Query vars to check for existence of redirect var.
* @return array<string> 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;
}

\wp_safe_redirect(
\trailingslashit( \get_option( 'home' ) ) . \user_trailingslashit( $query_vars['mytsf_category_redirect'], 'category' ),
301,
);
// 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;
}

// Fallback: redirect to home if not found
wp_safe_redirect( home_url(), 301 );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never do this! 404s are good. I smell a bit too much vibe 😅

exit;
}

/**
* Helper: Get category by slug.
*/
function get_category_by_slug( $slug ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function already exists in Core: https://developer.wordpress.org/reference/functions/get_category_by_slug/. It's heavy, though. If possible, I'd rather trim out the slug outright instead of performing category lookups.
In other words, we probably just want to modify the rewrite base so that /%category%/ disappears.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have at it... you're a better coder than me most likely... i'm out of the game mostly lol. Too much dust for me to knock off to code full-time anymore. I just want a solution that's solid and can be shared back to the community for everybody to use.

$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.
*
Expand Down