Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Adjusts pagination links by removing the `.html` extension from paginated URLs.
- *
- * @param string $link The original pagination link.
- * @return string The modified pagination link without `.html` before pagination numbers.
- */
- function pm_adjust_paginate_links( $link, $term = '' ) {
- global $pm_query;
- if ( ! empty( $pm_query['id'] ) && ! empty( $link ) ) {
- if ( ! empty( $term ) ) {
- $element = ( is_a( $term, 'WP_Term' ) ) ? $term : '';
- } else {
- $element = get_queried_object();
- }
- if ( ! empty( $element->term_id ) && is_a( $element, 'WP_Term' ) ) {
- if ( strpos( $link, '/page/' ) === false ) {
- $link = get_term_link( $element, $element->taxonomy );
- } else {
- $link = preg_replace( '/(.+)(\.html)(\/[^\/]+\/[\d]+.*)/', '$1$3', $link );
- }
- }
- }
- return $link;
- }
- add_filter( 'paginate_links', 'pm_adjust_paginate_links' );
- /**
- * Detects paginated links that should include `.html` for correct permalink matching.
- *
- * @param int|null $element_id The detected element ID.
- * @param array $uri_parts An array containing URI components.
- * @param string $request_url The full requested URL.
- * @return int|null The adjusted element ID if a match is found, otherwise the original element ID.
- */
- function pm_detect_paginate_links( $element_id, $uri_parts, $request_url ) {
- global $permalink_manager_uris;
- if ( empty( $element_id ) && ! empty( $uri_parts['uri'] ) && ! empty( $uri_parts['endpoint'] )
- && $uri_parts['endpoint'] == 'page' && strpos( $uri_parts['uri'], '.html' ) === false ) {
- $new_element_id = array_search( $uri_parts['uri'] . '.html', $permalink_manager_uris );
- }
- return ( ! empty( $new_element_id ) ) ? $new_element_id : $element_id;
- }
- add_filter( 'permalink_manager_detected_element_id', 'pm_detect_paginate_links', 20, 3 );
- /**
- * Redirects incorrectly formatted paginated links to the correct permalink structure.
- *
- * @global array $pm_query The query parameters managed by Permalink Manager.
- * @global WP $wp WordPress global request handler.
- */
- function pm_redirect_paginate_link() {
- global $pm_query, $wp;
- if ( ! empty( $pm_query['id'] ) && ! empty( $pm_query['uri'] ) && ! empty( $pm_query['endpoint'] )
- && $pm_query['endpoint'] == 'page' && strpos( $pm_query['uri'], '.html' ) !== false ) {
- $correct_url = pm_adjust_paginate_links( user_trailingslashit( home_url( $wp->request ) ) );
- wp_safe_redirect( $correct_url, 301 );
- exit();
- }
- }
- add_action( 'template_redirect', 'pm_redirect_paginate_link', 10, 6 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement