Advertisement
mbis

Rewrite products permalinks + guess 404 permalinks

Jul 2nd, 2021
1,858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Rewrite Product Permalinks
  5. Plugin URI: http://maciejbis.net
  6. Description: A plugin that adjusts the functionality of this website
  7. Version: 1.0
  8. Author: Maciej Bis
  9. Author URI: http://www.maciejbis.net
  10. License: GPL2
  11. */
  12.  
  13. /**
  14.  * Parse requested URL
  15.  */
  16. function bis_parse_requested_url() {
  17.     global $pm_query, $wp;
  18.    
  19.     if(empty($wp->request)) {
  20.         return;
  21.     }
  22.    
  23.     // Do not run when Elementor is opened
  24.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  25.         return;
  26.     }
  27.  
  28.     // Do not run if custom permalink was detected
  29.     if(!empty($pm_query['id'])) {
  30.         return;
  31.     }
  32.  
  33.     // 1. Get the slug
  34.     preg_match('/^\/?(?:product)(?:\/.*)?\/([^\/]+)\/([\d]+)$/', $wp->request, $parts);
  35.    
  36.     return $parts;
  37. }
  38.  
  39. /**
  40.  * Detect products permalinks
  41.  */
  42. function bis_detect_product_permalinks($query) {
  43.     global $wpdb;
  44.  
  45.     $parts = bis_parse_requested_url();
  46.  
  47.     if(!empty($parts[2])) {
  48.         // $slug = basename($parts[1]);
  49.         $product_id = (int) $parts[2];
  50.         $product = get_post($product_id);
  51.  
  52.         if(!empty($product)) {
  53.             $new_query = array(
  54.                 'post_type' => 'product',
  55.                 'product' => $product->post_name,
  56.                 'name' => $product->post_name,
  57.                 // 'page' => $page,
  58.                 'do_not_redirect' => 1,
  59.             );
  60.         }
  61.  
  62.         // 3. Overwrite the query object & Disable canonical redirect
  63.         if(!empty($new_query)) {
  64.             remove_action('template_redirect', 'wp_old_slug_redirect');
  65.             remove_action('template_redirect', 'redirect_canonical');
  66.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  67.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  68.  
  69.             $query = $new_query;
  70.         }
  71.     }
  72.  
  73.     return $query;
  74. }
  75. add_filter('request', 'bis_detect_product_permalinks', 9999);
  76.  
  77. /**
  78.  * Find a similar product
  79.  */
  80. function bis_guess_product() {
  81.     global $wpdb;
  82.    
  83.     if(is_404()) {
  84.         $parts = bis_parse_requested_url();
  85.        
  86.         if(!empty($parts[1])) {
  87.             $product_slug = preg_replace('/([^-]+-)(.*)(-[^-]+)/', '$2', $parts[1]);
  88.             $where = $wpdb->prepare('post_name LIKE %s', '%' . $wpdb->esc_like($product_slug) . '%');
  89.            
  90.             $product_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_type = 'product' AND post_status = 'publish'");
  91.             $product_permalink = get_permalink($product_id);
  92.            
  93.             if(!empty($product_permalink)) {
  94.                 wp_safe_redirect($product_permalink, 301, 'Bis');
  95.                 exit();
  96.             }
  97.         }
  98.     }
  99. }
  100. add_action('template_redirect', 'bis_guess_product');
  101.  
  102. /**
  103.  * Get taxonomy slug
  104.  */
  105. function bis_get_taxonomy_slug($post_id, $taxonomy) {
  106.     $taxonomy_slug = '';
  107.  
  108.     // Get the custom taxonomy terms in use by this post.
  109.     $terms = get_the_terms($post_id, $taxonomy);
  110.  
  111.     if(!empty($terms)) {
  112.         $terms = wp_list_sort(
  113.             $terms,
  114.             array(
  115.                 'parent'  => 'DESC',
  116.                 'term_id' => 'ASC',
  117.             )
  118.         );
  119.  
  120.         if(!empty($terms[0])) {
  121.             $taxonomy_slug = $terms[0]->slug;
  122.         }
  123.     }
  124.  
  125.     return $taxonomy_slug;
  126. }
  127.  
  128. /**
  129.  * Rewrite product permalinks
  130.  */
  131. function bis_filter_product_permalinks($url, $post, $leavename, $sample) {
  132.     if(!empty($post->post_type) && ($post->post_type == 'product')) {
  133.         $product_taxonomies = get_object_taxonomies($post);
  134.        
  135.         // New URL format
  136.         $new_url = 'product/%store%/%product%/%product_id%';
  137.  
  138.         // A. Replace taxonomy slugs
  139.         if($product_taxonomies) {
  140.             foreach($product_taxonomies as $taxonomy) {
  141.                 // Check if taxonomy tag is present
  142.                 if(strpos($new_url, "%{$taxonomy}") === false) { continue; }
  143.  
  144.                 $taxonomy_slug = bis_get_taxonomy_slug($post->ID, $taxonomy);
  145.  
  146.                 $new_url = str_replace("%{$taxonomy}%", $taxonomy_slug, $new_url);
  147.             }
  148.         }
  149.        
  150.         // B. Replace product ID
  151.         $new_url = str_replace("%product_id%", $post->ID, $new_url);
  152.        
  153.         // C. Replace product slug
  154.         $new_url = str_replace("%product%", $post->post_name, $new_url);
  155.  
  156.         $url = sprintf('%s/%s', trim(get_option('home'), "/"), $new_url);
  157.         $url = user_trailingslashit($url);
  158.     }
  159.  
  160.     return $url;
  161. }
  162. add_filter('post_type_link', 'bis_filter_product_permalinks', 10, 4);
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement