Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detect posts
- */
- function pm_detect_woo_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;
- }
- // 1. Get the slug
- preg_match("/([^\/]+)(?:\/{$wp_rewrite->pagination_base}\/([\d]+))?$/", $wp->request, $parts);
- if(!empty($parts[1])) {
- $slug = basename($parts[1]);
- // 2A. Check if the slug is assigned to any post item
- $sql_query = "SELECT post_name, post_type FROM {$wpdb->posts} WHERE 1=1 AND post_name = %s AND post_type = 'product'";
- $post = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));
- if(!empty($post->post_name)) {
- $page = (!empty($query['page'])) ? $query['page'] : 1;
- $new_query = array(
- 'post_type' => $post->post_type,
- $post->post_type => $post->post_name,
- 'name' => $post->post_name,
- // 'page' => $page,
- 'do_not_redirect' => 1,
- );
- }
- // 2B. Check if the slug is assigned to any post item
- else {
- $sql_query = "SELECT t.slug, t.term_id, tt.taxonomy FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON(t.term_id = tt.term_id) WHERE 1=1 AND slug = %s";
- $term = $wpdb->get_row($wpdb->prepare($sql_query, array($slug)));
- if(!empty($term->taxonomy)) {
- $page = (!empty($parts[2])) ? (int) $parts[2] : 1;
- $new_query = array(
- 'term' => $term->slug,
- $term->taxonomy => $term->slug,
- 'taxonomy' => $term->taxonomy,
- 'paged' => $page
- );
- }
- }
- // 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;
- }
- }
- return $query;
- }
- add_filter('request', 'pm_detect_woo_permalinks', 9999);
- function bis_filter_woo_permalinks($url, $element) {
- if(!empty($element->post_type) && ($element->post_type == 'product')) {
- $url = trim(get_option('home'), '/') . "/{$element->post_name}";
- } else if(!empty($element->taxonomy) && (in_array($element->taxonomy, array('product_cat', 'product_tag')))) {
- $woocommerce_permalinks = get_option('woocommerce_permalinks');
- $url = (!empty($woocommerce_permalinks['category_base'])) ? str_replace(array("{$woocommerce_permalinks['category_base']}/", "{$woocommerce_permalinks['tag_base']}/"), '', $url) : $url;
- }
- return $url;
- }
- add_filter('post_type_link', 'bis_filter_woo_permalinks', 999, 2);
- add_filter('term_link', 'bis_filter_woo_permalinks', 999, 2);
- function pm_redirect_old_woo_permalinks() {
- global $wp, $wp_query, $wp_rewrite;
- if(empty($wp->request)) { return; }
- $woocommerce_permalinks = get_option('woocommerce_permalinks');
- // A. Redirect old product permalinks
- if(is_singular('product') && !empty($woocommerce_permalinks['product_base'])) {
- $product_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['product_base']), "/");
- if(preg_match("/^({$product_permalink_base})/", $wp->request)) {
- $url = get_permalink();
- }
- }
- // B. Redirect old category permalinks
- if(is_tax('product_cat') && !empty($woocommerce_permalinks['category_base'])) {
- $product_cat_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['category_base']), "/");
- if(preg_match("/^({$product_cat_permalink_base})/", $wp->request)) {
- $term = get_queried_object();
- $url = get_term_link($term, $term->taxonomy);
- }
- }
- // C. Redirect old tag permalinks
- if(is_tax('product_tag') && !empty($woocommerce_permalinks['tag_base'])) {
- $product_tag_permalink_base = preg_quote(preg_replace('/(%[^%]+%)/', '', $woocommerce_permalinks['tag_base']), "/");
- if(preg_match("/^({$product_tag_permalink_base})/", $wp->request)) {
- $term = get_queried_object();
- $url = get_term_link($term, $term->taxonomy);
- }
- }
- // Do the redirect
- if(!empty($url)) {
- // Support pagination
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
- if(!empty($paged) && $paged > 1) {
- $url = sprintf("%s/%s/%s", trim($url, "/"), $wp_rewrite->pagination_base, $paged);
- $url = user_trailingslashit($url);
- }
- wp_safe_redirect($url, 301);
- exit();
- }
- }
- add_action('template_redirect', 'pm_redirect_old_woo_permalinks', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement