Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detect product permalinks
- */
- function pm_detect_post_slugs($query) {
- global $wpdb, $pm_query;
- if(!empty($pm_query['uri'])) {
- $post_name = basename($pm_query['uri']);
- $sql_query = $wpdb->prepare(
- "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = 'product'",
- array($post_name)
- );
- $uri = $wpdb->get_var($sql_query);
- if(!empty($uri)) {
- $page = (!empty($query['page'])) ? $query['page'] : 1;
- $new_query = array(
- 'product' => $uri,
- 'post_type' => 'product',
- 'name' => $uri,
- 'page' => $page,
- 'do_not_redirect' => 1,
- );
- // Disable canonical redirect
- 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);
- return $new_query;
- }
- }
- return $query;
- }
- add_filter('request', 'pm_detect_post_slugs');
- /**
- * Filter product permalinks
- */
- function bis_filter_product_permalinks($url, $element) {
- if(!empty($element->post_type) && ($element->post_type == 'product')) {
- $home_base = trim(get_option('home'), '/');
- // $url = preg_replace('/(http(?:s)?:\/\/(?:www\.)?[^\/]+\/)([^\/]+)\/([^\/]+)/', '$1$3', $url);
- $url = "{$home_base}/{$element->post_name}";
- }
- return user_trailingslashit($url);
- }
- add_filter('post_type_link', 'bis_filter_product_permalinks', 999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement