-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 ); | ||
exit; | ||
} | ||
|
||
/** | ||
* Helper: Get category by slug. | ||
*/ | ||
function get_category_by_slug( $slug ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
There was a problem hiding this comment.
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 😅