-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(categories): fix pager urls (#2913)
* fix(categories): fix pager urls On a permalink structure like this /%category%/%postname%/ some fixes to the pager was needed. Also guard against a post with the slug 'page' to avoid confusion to routing. * fix(categories): fix pager urls Don't hardcode category base * Don't hardcode category base and remove unneeded filter * fix(categories): fix pager urls * PR feedback adressed * Use 'category' as default if base is empty --------- Co-authored-by: Adam Boro <aborowski24@gmail.com>
- Loading branch information
1 parent
4df9e9d
commit bb7e534
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Newspack category pager fix. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Newspack category pager. | ||
* | ||
* This class adds tweaks to sites where the permalink structure is "/%category%/%postname%/". | ||
* | ||
* See https://core.trac.wordpress.org/ticket/8905 and https://core.trac.wordpress.org/ticket/21209 | ||
*/ | ||
final class Category_Pager { | ||
/** | ||
* Initialize Hooks. | ||
*/ | ||
public static function init() { | ||
add_filter( 'paginate_links', [ __CLASS__, 'fix_category_pager' ] ); | ||
} | ||
|
||
/** | ||
* Filter callback for paginate_links. | ||
* | ||
* Filter the pager urls on category pages. | ||
* | ||
* For a category with the slug "kitten" for instance, the url /kitten will show the content that | ||
* really should be on /category/kitten. On the /kitten page, WP will generate pager urls like "/kitten/page/2/" | ||
* – this fixes the pager urls to be "/category/kitten/page/2/". | ||
* | ||
* @param string $link The pager link to filter. | ||
* | ||
* @return mixed|string | ||
*/ | ||
public static function fix_category_pager( $link ) { | ||
if ( ! is_category() || ! str_starts_with( get_option( 'permalink_structure' ), '/%category%/' ) ) { | ||
return $link; | ||
} | ||
|
||
$category_base = get_option( 'category_base' ); | ||
if ( empty( $category_base ) ) { | ||
$category_base = 'category'; | ||
} | ||
|
||
$path = wp_parse_url( $link, PHP_URL_PATH ); | ||
if ( str_starts_with( $path, '/' . $category_base ) ) { | ||
// No fixing needed. | ||
return $link; | ||
} | ||
|
||
$new_path = '/' . $category_base . $path; | ||
|
||
return str_replace( $path, $new_path, $link ); | ||
} | ||
} | ||
|
||
Category_Pager::init(); |
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