Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Remove /store/ prefix
- **/
- function pm_detect_store_permalinks($query) {
- global $wpdb, $pm_query, $wp, $wp_rewrite;
- // Do not run when Elementor is opened
- if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
- return $query;
- }
- // Do not run if custom permalink was detected
- if(!empty($pm_query['id'])) {
- return $query;
- }
- // Get all available endpoints
- $endpoints = 'section|toc';
- if(!empty($wp_rewrite->endpoints)) {
- foreach($wp_rewrite->endpoints as $endpoint) {
- $endpoints .= "|{$endpoint[1]}";
- }
- }
- // 1. Get the store slug (& endpoint if any)
- preg_match("/^(^[^\/]+)(?:\/($endpoints))?/", $wp->request, $parts);
- if(!empty($parts[1])) {
- $store_name = basename($parts[1]);
- // 2. Check if the slug is assigned to any vendor
- $vendor = get_user_by('slug', $store_name);
- if(!empty($vendor->user_nicename)) {
- $new_query = array(
- 'shop' => $vendor->user_nicename,
- 'do_not_redirect' => 1,
- );
- // Set the endpoint
- if(!empty($parts[2])) {
- $new_query[$parts[2]] = true;
- }
- }
- // 3. Overwrite the query object & disable canonical redirect
- if(!empty($new_query)) {
- remove_action('template_redirect', 'wp_old_slug_redirect');
- remove_action('template_redirect', 'redirect_canonical');
- add_filter('wpml_is_redirected', '__return_false', 99, 2);
- add_filter('pll_check_canonical_url', '__return_false', 99, 2);
- $query = $new_query;
- if(isset($_GET['debug_query'])) {
- echo '<pre>';
- print_r($query);
- print_r($new_query);
- print_r($endpoints);
- print_r($parts);
- echo '</pre>';
- }
- }
- }
- return $query;
- }
- add_filter('request', 'pm_detect_store_permalinks', 9999);
- function pm_remove_store_base($url) {
- $store_prefix = dokan_get_option('custom_store_url', 'dokan_general', 'store' );
- return (strpos($url, 'shop') !== false) ? preg_replace('/(^|\/)(' . preg_quote($store_prefix, '/') . ')\/?/', '', $url) : $url;
- }
- add_filter('clean_url', 'pm_remove_store_base', 999);
- add_filter('home_url', 'pm_remove_store_base', 999);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement