Advertisement
mbis

Remove .html from paginated links

Feb 16th, 2025 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. /**
  2.  * Adjusts pagination links by removing the `.html` extension from paginated URLs.
  3.  *
  4.  * @param string $link The original pagination link.
  5.  * @return string The modified pagination link without `.html` before pagination numbers.
  6.  */
  7. function pm_adjust_paginate_links( $link, $term = '' ) {
  8.     global $pm_query;
  9.  
  10.     if ( ! empty( $pm_query['id'] ) && ! empty( $link ) ) {
  11.         if ( ! empty( $term ) ) {
  12.             $element = ( is_a( $term, 'WP_Term' ) ) ? $term : '';
  13.         } else {
  14.             $element = get_queried_object();
  15.         }
  16.  
  17.         if ( ! empty( $element->term_id ) && is_a( $element, 'WP_Term' ) ) {
  18.             if ( strpos( $link, '/page/' ) === false ) {
  19.                 $link = get_term_link( $element, $element->taxonomy );
  20.             } else {
  21.                 $link = preg_replace( '/(.+)(\.html)(\/[^\/]+\/[\d]+.*)/', '$1$3', $link );
  22.             }
  23.         }
  24.     }
  25.  
  26.     return $link;
  27. }
  28. add_filter( 'paginate_links', 'pm_adjust_paginate_links' );
  29.  
  30. /**
  31.  * Detects paginated links that should include `.html` for correct permalink matching.
  32.  *
  33.  * @param int|null $element_id The detected element ID.
  34.  * @param array $uri_parts An array containing URI components.
  35.  * @param string $request_url The full requested URL.
  36.  * @return int|null The adjusted element ID if a match is found, otherwise the original element ID.
  37.  */
  38. function pm_detect_paginate_links( $element_id, $uri_parts, $request_url ) {
  39.     global $permalink_manager_uris;
  40.  
  41.     if ( empty( $element_id ) && ! empty( $uri_parts['uri'] ) && ! empty( $uri_parts['endpoint'] )
  42.         && $uri_parts['endpoint'] == 'page' && strpos( $uri_parts['uri'], '.html' ) === false ) {
  43.         $new_element_id = array_search( $uri_parts['uri'] . '.html', $permalink_manager_uris );
  44.     }
  45.  
  46.     return ( ! empty( $new_element_id ) ) ? $new_element_id : $element_id;
  47. }
  48. add_filter( 'permalink_manager_detected_element_id', 'pm_detect_paginate_links', 20, 3 );
  49.  
  50. /**
  51.  * Redirects incorrectly formatted paginated links to the correct permalink structure.
  52.  *
  53.  * @global array $pm_query The query parameters managed by Permalink Manager.
  54.  * @global WP $wp WordPress global request handler.
  55.  */
  56. function pm_redirect_paginate_link() {
  57.     global $pm_query, $wp;
  58.  
  59.     if ( ! empty( $pm_query['id'] ) && ! empty( $pm_query['uri'] ) && ! empty( $pm_query['endpoint'] )
  60.         && $pm_query['endpoint'] == 'page' && strpos( $pm_query['uri'], '.html' ) !== false ) {
  61.         $correct_url = pm_adjust_paginate_links( user_trailingslashit( home_url( $wp->request ) ) );
  62.  
  63.         wp_safe_redirect( $correct_url, 301 );
  64.         exit();
  65.     }
  66. }
  67. add_action( 'template_redirect', 'pm_redirect_paginate_link', 10, 6 );
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement