Advertisement
mbis

WooCommerce product permalinks (.html)

Nov 22nd, 2022 (edited)
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. /**
  2.  * Detect products & categories
  3.  */
  4. function bis_detect_product_permalink($query) {
  5.     global $wpdb, $pm_query, $wp;
  6.  
  7.     // Do not run when Elementor is opened
  8.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  9.       return $query;
  10.     }
  11.  
  12.     // Do not run if custom permalink was detected
  13.     if(!empty($pm_query['id'])) {
  14.       return $query;
  15.     }
  16.  
  17.     // 1. Get the slug
  18.     if(!empty($wp->request) && strpos($wp->request, '.html') !== false) {
  19.         $slug = basename($wp->request);
  20.  
  21.                 // 2. Get rid off .html extension
  22.                 $slug = str_replace('.html', '', $slug);
  23.  
  24.         // 3. Check if the slug is assigned to any post item
  25.         $sql_query = "SELECT post_name, post_type FROM {$wpdb->posts} WHERE 1=1 AND post_name = %s AND post_type = 'product'";
  26.         $post = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));
  27.  
  28.         if(!empty($post->post_name)) {
  29.             $page = (!empty($query['page'])) ? $query['page'] : 1;
  30.  
  31.             $new_query = array(
  32.                 'post_type' => $post->post_type,
  33.                 $post->post_type => $post->post_name,
  34.                 'name' => $post->post_name,
  35.                 'do_not_redirect' => 1,
  36.             );
  37.         }
  38.  
  39.         // 4. Overwrite the query object & Disable canonical redirect
  40.         if(!empty($new_query)) {
  41.             remove_action('template_redirect', 'wp_old_slug_redirect');
  42.             remove_action('template_redirect', 'redirect_canonical');
  43.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  44.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  45.  
  46.             $query = $new_query;
  47.         }
  48.     }
  49.  
  50.         if(isset($_GET['debug_query'])) {
  51.             echo sprintf('<pre>Old query: %s</pre>', print_r($query, true));
  52.             echo (!empty($new_query)) ? sprintf('<pre>New query: %s</pre>', print_r($new_query, true)) : '';
  53.             echo (!empty($post)) ? sprintf('<pre>Element: %s</pre>', print_r($post, true)) : '';
  54.             die();
  55.         }
  56.  
  57.     return $query;
  58. }
  59. add_filter('request', 'bis_detect_product_permalink', 9999);
  60.  
  61. function bis_filter_product_permalink($url, $element) {
  62.     if(!empty($element->post_type) && ($element->post_type == 'product')) {
  63.         $new_url = preg_replace('/(http(s)?:\/\/[^\/]+)\/(.*)/', '$1/' . $element->post_name . '.html', $url);
  64.     }
  65.  
  66.     return (!empty($new_url)) ? $new_url : $url;
  67. }
  68. add_filter('post_type_link', 'bis_filter_product_permalink', 999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement