Advertisement
mbis

Add .html to product permalinks

Jan 7th, 2021
2,206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. /**
  2.  * Add .html extension to product permalinks
  3.  */
  4. function bis_detect_html_permalinks($query) {
  5.   global $wpdb, $pm_query, $wp, $wp_rewrite;
  6.  
  7.   // Do not run if custom permalink was detected
  8.   if(!empty($pm_query['id'])) {
  9.     return $query;
  10.   }
  11.  
  12.   if(!empty($query['product']) && strpos($query['product'], '.html') !== false) {
  13.         $query['product'] = str_replace('.html', '', $query['product']);
  14.  
  15.         if(!empty($query['name'])) {
  16.             $query['name'] = $query['product'];
  17.         }
  18.  
  19.     // Disable canonical redirect
  20.     remove_action('template_redirect', 'wp_old_slug_redirect');
  21.     remove_action('template_redirect', 'redirect_canonical');
  22.     add_filter('wpml_is_redirected', '__return_false', 99, 2);
  23.     add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  24.   }
  25.  
  26.   return $query;
  27. }
  28. add_filter('request', 'bis_detect_html_permalinks', 9999);
  29.  
  30. function bis_filter_product_permalinks($url, $element) {
  31.     if(!empty($element->post_type) && ($element->post_type == 'product')) {
  32.         $url .= ".html";
  33.     }
  34.  
  35.     return $url;
  36. }
  37. add_filter('post_type_link', 'bis_filter_product_permalinks', 999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement