simkoG

remove-product-category-slug.php

Aug 5th, 2020 (edited)
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  2.  
  3. /*!
  4.   Plugin Name: Remove /product/, /shop/ and /product-category/ from URL
  5.   Plugin
  6.   Description: A fent nevezett részek eltávolítása az URL-bol
  7.   Version: 1.0
  8.   Author:
  9.   Author URI:
  10.   License: GPL2
  11. */
  12.  
  13. /*
  14. * Remove /product/ or /shop/ ... support %product_cat%
  15. */
  16. function wccustom_remove_slug( $post_link, $post ) {
  17.     if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) {
  18.         return $post_link;
  19.     }
  20.     if('product' == $post->post_type){
  21.         $post_link = str_replace( '/termek/', '/', $post_link ); //replace "product" to your slug
  22.     }else{
  23.         $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  24.     }
  25.     return $post_link;
  26. }
  27. add_filter( 'post_type_link', 'wccustom_remove_slug', 10, 2 );
  28.  
  29. function wccustom_woo_product_rewrite_rules($flash = false) {
  30.     global $wp_post_types, $wpdb;
  31.     $siteLink = esc_url(home_url('/'));
  32.     foreach ($wp_post_types as $type=>$custom_post) {
  33.         if($type == 'product'){
  34.             if ($custom_post->_builtin == false) {
  35.                 $querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID
  36.                            FROM {$wpdb->posts}
  37.                            WHERE {$wpdb->posts}.post_status = 'publish'
  38.                            AND {$wpdb->posts}.post_type = '{$type}'";
  39.                 $posts = $wpdb->get_results($querystr, OBJECT);
  40.                 foreach ($posts as $post) {
  41.                     $current_slug = get_permalink($post->ID);
  42.                     $base_product = str_replace($siteLink,'',$current_slug);
  43.                     add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top');
  44.                 }
  45.             }
  46.         }
  47.     }
  48.     if ($flash == true)
  49.         flush_rewrite_rules(false);
  50. }
  51. add_action( 'init', 'wccustom_woo_product_rewrite_rules' );
  52.  
  53. /*Fix 404*/
  54. function wccustom_woo_new_product_post_save($post_id){
  55.     global $wp_post_types;
  56.     $post_type = get_post_type($post_id);
  57.     foreach ($wp_post_types as $type=>$custom_post) {
  58.         if ($custom_post->_builtin == false && $type == $post_type) {
  59.             wccustom_woo_product_rewrite_rules(true);
  60.         }
  61.     }
  62. }
  63. add_action( 'wp_insert_post', 'wccustom_woo_new_product_post_save' );
  64.  
  65. /*
  66. * Remove product-category
  67. */
  68. add_filter( 'term_link', 'wccustom_product_cat_permalink', 10, 3 );
  69. function wccustom_product_cat_permalink( $url, $term, $taxonomy ){
  70.     switch ($taxonomy):
  71.         case 'product_cat':
  72.             $taxonomy_slug = 'termek_kategoria'; // Change product-category to your product category slug
  73.             if(strpos($url, $taxonomy_slug) === FALSE) break;
  74.             $url = str_replace('/' . $taxonomy_slug, '', $url);
  75.             break;
  76.     endswitch;
  77.     return $url;
  78. }
  79.  
  80. // Add our custom product cat rewrite rules
  81. function wccustom_product_category_rewrite_rules($flash = false) {
  82.     $terms = get_terms( array(
  83.         'taxonomy' => 'product_cat',
  84.         'post_type' => 'product',
  85.         'hide_empty' => false,
  86.     ));
  87.     if($terms && !is_wp_error($terms)){
  88.         $siteurl = esc_url(home_url('/'));
  89.         foreach ($terms as $term){
  90.             $term_slug = $term->slug;
  91.             $baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat'));
  92.             add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top');
  93.             add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top');
  94.             add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top');
  95.         }
  96.     }
  97.     if ($flash == true)
  98.         flush_rewrite_rules(false);
  99. }
  100. add_action('init', 'wccustom_product_category_rewrite_rules');
  101.  
  102. /*Fix 404 when creat new term*/
  103. add_action( 'create_term', 'wccustom_new_product_cat_edit_success', 10, 2 );
  104. function wccustom_new_product_cat_edit_success( $term_id, $taxonomy ) {
  105.     wccustom_product_category_rewrite_rules(true);
  106. }
  107. ?>
Add Comment
Please, Sign In to add comment