Advertisement
mbis

Remove /shop/ base from Dokan URLs

Aug 20th, 2020
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1.  
  2. /**
  3.  * Remove /store/ prefix
  4.  **/
  5. function pm_detect_store_permalinks($query) {
  6.     global $wpdb, $pm_query, $wp, $wp_rewrite;
  7.  
  8.     // Do not run when Elementor is opened
  9.     if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
  10.         return $query;
  11.     }
  12.  
  13.     // Do not run if custom permalink was detected
  14.     if(!empty($pm_query['id'])) {
  15.         return $query;
  16.     }
  17.    
  18.     // Get all available endpoints
  19.     $endpoints = 'section|toc';
  20.     if(!empty($wp_rewrite->endpoints)) {
  21.         foreach($wp_rewrite->endpoints as $endpoint) {
  22.             $endpoints .= "|{$endpoint[1]}";
  23.         }
  24.     }
  25.  
  26.     // 1. Get the store slug (& endpoint if any)
  27.     preg_match("/^(^[^\/]+)(?:\/($endpoints))?/", $wp->request, $parts);
  28.  
  29.     if(!empty($parts[1])) {
  30.         $store_name = basename($parts[1]);
  31.  
  32.         // 2. Check if the slug is assigned to any vendor
  33.         $vendor = get_user_by('slug', $store_name);
  34.  
  35.         if(!empty($vendor->user_nicename)) {
  36.             $new_query = array(
  37.                 'shop' => $vendor->user_nicename,
  38.                 'do_not_redirect' => 1,
  39.             );
  40.            
  41.             // Set the endpoint
  42.             if(!empty($parts[2])) {
  43.                 $new_query[$parts[2]] = true;
  44.             }
  45.         }
  46.  
  47.         // 3. Overwrite the query object & disable canonical redirect
  48.         if(!empty($new_query)) {
  49.             remove_action('template_redirect', 'wp_old_slug_redirect');
  50.             remove_action('template_redirect', 'redirect_canonical');
  51.             add_filter('wpml_is_redirected', '__return_false', 99, 2);
  52.             add_filter('pll_check_canonical_url', '__return_false', 99, 2);
  53.  
  54.             $query = $new_query;
  55.  
  56.             if(isset($_GET['debug_query'])) {
  57.                 echo '<pre>';
  58.                 print_r($query);
  59.                 print_r($new_query);
  60.                 print_r($endpoints);
  61.                 print_r($parts);
  62.                 echo '</pre>';
  63.             }
  64.         }
  65.     }
  66.  
  67.     return $query;
  68. }
  69. add_filter('request', 'pm_detect_store_permalinks', 9999);
  70.  
  71. function pm_remove_store_base($url) {
  72.     $store_prefix = dokan_get_option('custom_store_url', 'dokan_general', 'store' );
  73.    
  74.     return (strpos($url, 'shop') !== false) ? preg_replace('/(^|\/)(' . preg_quote($store_prefix, '/') . ')\/?/', '', $url) : $url;
  75. }
  76. add_filter('clean_url', 'pm_remove_store_base', 999);
  77. add_filter('home_url', 'pm_remove_store_base', 999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement