-
-
Save aahan/b33085c0e6e3a4941c01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Related core file: wp-includes/canonical.php | |
* | |
* NOTE: If you see no change after adding the function, try re-saving the permalink settings. | |
*/ | |
add_filter( 'redirect_canonical', 'itsme_redirect_canonical', 10, 2 ); | |
function itsme_redirect_canonical( $redirect_url, $requested_url ) { | |
global $wp_rewrite; | |
if( is_single() && in_category( 'intl' ) && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { | |
$category = get_category_by_path( $cat ); | |
if( $category && !is_wp_error( $category ) ) { | |
return str_replace( 'intl', $cat, $redirect_url ); | |
// return $requested_url; | |
/* | |
* WHY NOT SIMPLY `return $requested_url;`? | |
* | |
* By returning $requested_url you're returning something that might not be | |
* canonical (e.g. extra slashes, etc) and there's a possibility that the | |
* safety rules that WordPress has in place will redirect your URL back to | |
* the (original) canonical URL. | |
* | |
* For example, this will work fine: | |
* | |
* http://example.com/in/2013/11/02/sample-post/ | |
* | |
* But this: | |
* | |
* http://example.com/in/2013/11/02/sample-post///// | |
* | |
* Will redirect back to the original, canonical URL, i.e., | |
* | |
* http://example.com/intl/2013/11/02/sample-post/ | |
* | |
* In order to avoid that, you'll additionally have to write some safety | |
* rules so that a *corrected* URL, rather than simply the requested one, is | |
* returned, in which case, you may need to use `str_replace` or `preg_replace` | |
* to remove the extra characters, among other rules needed, if any. Now that | |
* can get complex. | |
* | |
* So instead of dealing with all this mess ourselves, we are allowing | |
* WordPress to redirect to the (original) canonical URL, at which point, | |
* we are replacing 'intl' (which is the category slug in the canonical URL) | |
* with the category slug in the requested URL. | |
*/ | |
} | |
} | |
return $redirect_url; | |
} | |
// THE SAME CAN BE SAFELY WRITTEN AS (THE WAY IT IS DONE IN CORE FILE) | |
add_filter( 'redirect_canonical', 'itsme_redirect_canonical', 10, 2 ); | |
function itsme_redirect_canonical( $redirect_url, $requested_url ) { | |
global $wp_rewrite; | |
if( is_single() && in_category( 'intl' ) && strpos( $wp_rewrite->permalink_structure, '%category%' ) !== false && $cat = get_query_var( 'category_name' ) ) { | |
$category = get_category_by_path( $cat ); | |
if( !$category || is_wp_error( $category ) ) { | |
return $redirect_url; | |
} else { | |
return str_replace( 'intl', $cat, $redirect_url ); | |
} | |
} else { | |
return $redirect_url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment