Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Rewrite Product Permalinks
- Plugin URI: http://maciejbis.net
- Description: A plugin that adjusts the functionality of this website
- Version: 1.0
- Author: Maciej Bis
- Author URI: http://www.maciejbis.net
- License: GPL2
- */
- /**
- * Parse requested URL
- */
- function bis_parse_requested_url() {
- global $pm_query, $wp;
- if(empty($wp->request)) {
- return;
- }
- // Do not run when Elementor is opened
- if((!empty($_REQUEST['action']) && strpos($_REQUEST['action'], 'elementor') !== false) || isset($_REQUEST['elementor-preview'])) {
- return;
- }
- // Do not run if custom permalink was detected
- if(!empty($pm_query['id'])) {
- return;
- }
- // 1. Get the slug
- preg_match('/^\/?(?:product)(?:\/.*)?\/([^\/]+)\/([\d]+)$/', $wp->request, $parts);
- return $parts;
- }
- /**
- * Detect products permalinks
- */
- function bis_detect_product_permalinks($query) {
- global $wpdb;
- $parts = bis_parse_requested_url();
- if(!empty($parts[2])) {
- // $slug = basename($parts[1]);
- $product_id = (int) $parts[2];
- $product = get_post($product_id);
- if(!empty($product)) {
- $new_query = array(
- 'post_type' => 'product',
- 'product' => $product->post_name,
- 'name' => $product->post_name,
- // 'page' => $page,
- 'do_not_redirect' => 1,
- );
- }
- // 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', 'bis_detect_product_permalinks', 9999);
- /**
- * Find a similar product
- */
- function bis_guess_product() {
- global $wpdb;
- if(is_404()) {
- $parts = bis_parse_requested_url();
- if(!empty($parts[1])) {
- $product_slug = preg_replace('/([^-]+-)(.*)(-[^-]+)/', '$2', $parts[1]);
- $where = $wpdb->prepare('post_name LIKE %s', '%' . $wpdb->esc_like($product_slug) . '%');
- $product_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_type = 'product' AND post_status = 'publish'");
- $product_permalink = get_permalink($product_id);
- if(!empty($product_permalink)) {
- wp_safe_redirect($product_permalink, 301, 'Bis');
- exit();
- }
- }
- }
- }
- add_action('template_redirect', 'bis_guess_product');
- /**
- * Get taxonomy slug
- */
- function bis_get_taxonomy_slug($post_id, $taxonomy) {
- $taxonomy_slug = '';
- // Get the custom taxonomy terms in use by this post.
- $terms = get_the_terms($post_id, $taxonomy);
- if(!empty($terms)) {
- $terms = wp_list_sort(
- $terms,
- array(
- 'parent' => 'DESC',
- 'term_id' => 'ASC',
- )
- );
- if(!empty($terms[0])) {
- $taxonomy_slug = $terms[0]->slug;
- }
- }
- return $taxonomy_slug;
- }
- /**
- * Rewrite product permalinks
- */
- function bis_filter_product_permalinks($url, $post, $leavename, $sample) {
- if(!empty($post->post_type) && ($post->post_type == 'product')) {
- $product_taxonomies = get_object_taxonomies($post);
- // New URL format
- $new_url = 'product/%store%/%product%/%product_id%';
- // A. Replace taxonomy slugs
- if($product_taxonomies) {
- foreach($product_taxonomies as $taxonomy) {
- // Check if taxonomy tag is present
- if(strpos($new_url, "%{$taxonomy}") === false) { continue; }
- $taxonomy_slug = bis_get_taxonomy_slug($post->ID, $taxonomy);
- $new_url = str_replace("%{$taxonomy}%", $taxonomy_slug, $new_url);
- }
- }
- // B. Replace product ID
- $new_url = str_replace("%product_id%", $post->ID, $new_url);
- // C. Replace product slug
- $new_url = str_replace("%product%", $post->post_name, $new_url);
- $url = sprintf('%s/%s', trim(get_option('home'), "/"), $new_url);
- $url = user_trailingslashit($url);
- }
- return $url;
- }
- add_filter('post_type_link', 'bis_filter_product_permalinks', 10, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement