Last active
August 10, 2021 04:41
-
-
Save levantoan/a10876ceaee4324cdeb6ae18af11487a to your computer and use it in GitHub Desktop.
Xóa bỏ product trong đường dẫn của sản phẩm Xem chi tiết tại http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'term_link', 'plugins_remove_category_from_url', 10, 3); | |
function plugins_remove_category_from_url($termlink, $term, $taxonomy){ | |
if($taxonomy == 'product_cat'){ | |
$termlink = str_replace('/'._x( 'product-category', 'slug', 'woocommerce' ).'/', '/', $termlink); | |
} | |
return $termlink; | |
} | |
add_action('init', 'devvn_product_category_rewrite_rules'); | |
function devvn_product_category_rewrite_rules($flash = false) | |
{ | |
add_rewrite_rule('(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=$matches[1]&feed=$matches[2]', 'bottom'); | |
add_rewrite_rule('(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=$matches[1]&feed=$matches[2]', 'bottom'); | |
add_rewrite_rule('(.+?)/embed/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'bottom'); | |
add_rewrite_rule('(.+?)/page/?([0-9]{1,})/?$', 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'bottom'); | |
add_rewrite_rule('(.+?)/?$', 'index.php?product_cat=$matches[1]', 'bottom'); | |
if($flash == true){ | |
flush_rewrite_rules(false); | |
} | |
} | |
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 ); | |
add_action( 'edit_term', 'devvn_new_product_cat_edit_success' ); | |
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) { | |
devvn_product_category_rewrite_rules(true); | |
} | |
if(!is_admin()){ | |
add_filter('request', 'taxonomy_change_term_request', 1, 1 ); | |
function taxonomy_change_term_request($query){ | |
global $wp_rewrite; | |
/*$rewrite = $wp_rewrite->wp_rewrite_rules(); | |
echo '<pre>'; | |
print_r($rewrite); | |
echo '</pre>';*/ | |
if(isset($query)){ | |
if(isset($query['pagename'])) { | |
$name = $query['pagename']; | |
$term = get_term_by('slug', $name, 'product_cat'); | |
if (isset($name) && $term && !is_wp_error($term)) { | |
unset($query['pagename']); | |
$query['product_cat'] = $name; | |
} | |
return $query; | |
}; | |
if(isset($query['name'])) { | |
$name = $query['name']; | |
$term = get_term_by('slug', $name, 'product_cat'); | |
if (isset($name) && $term && !is_wp_error($term)) { | |
unset($query['name']); | |
$query['product_cat'] = $name; | |
} | |
return $query; | |
}; | |
if(isset($query['attachment'])): | |
$name = $query['attachment']; | |
$term = get_term_by('slug', $name, 'product_cat'); | |
if (isset($name) && $term && !is_wp_error($term)) { | |
unset($query['attachment']); | |
$query['product_cat'] = $name; | |
} | |
endif; | |
return $query; | |
} | |
} | |
} | |
add_action('template_redirect', 'plugins_product_cat_old_term_redirect'); | |
function plugins_product_cat_old_term_redirect() { | |
$taxonomy_name = 'product_cat'; | |
$taxonomy_slug = _x( 'product-category', 'slug', 'woocommerce' ); | |
if (is_tax($taxonomy_name) && strpos($_SERVER['REQUEST_URI'], $taxonomy_slug) ) : | |
wp_redirect(site_url(str_replace($taxonomy_slug, '', $_SERVER['REQUEST_URI'])), 301); | |
exit(); | |
endif; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Remove product-category in URL | |
* Thay product-category bằng slug hiện tại của bạn. Mặc định là product-category | |
*/ | |
add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 ); | |
function devvn_product_cat_permalink( $url, $term, $taxonomy ){ | |
switch ($taxonomy): | |
case 'product_cat': | |
$taxonomy_slug = 'product-category'; //Thay bằng slug hiện tại của bạn. Mặc định là product-category | |
if(strpos($url, $taxonomy_slug) === FALSE) break; | |
$url = str_replace('/' . $taxonomy_slug, '', $url); | |
break; | |
endswitch; | |
return $url; | |
} | |
// Add our custom product cat rewrite rules | |
add_filter('rewrite_rules_array', 'devvn_product_category_rewrite_rules'); | |
function devvn_product_category_rewrite_rules($rules) { | |
$new_rules = array(); | |
$terms = get_terms( array( | |
'taxonomy' => 'product_cat', | |
'post_type' => 'product', | |
'hide_empty' => false, | |
)); | |
if($terms && !is_wp_error($terms)){ | |
$siteurl = esc_url(home_url('/')); | |
foreach ($terms as $term){ | |
$term_slug = $term->slug; | |
$baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat')); | |
$new_rules[$baseterm.'?$'] = 'index.php?product_cat='.$term_slug; | |
$new_rules[$baseterm.'page/([0-9]{1,})/?$'] = 'index.php?product_cat='.$term_slug.'&paged=$matches[1]'; | |
$new_rules[$baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?product_cat='.$term_slug.'&feed=$matches[1]'; | |
} | |
} | |
return $new_rules + $rules; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Remove product form url | |
* Chú ý Thay product bằng slug hiện tại của bạn | |
* Link hướng dẫn cụ thể: http://levantoan.com/huong-dan-xoa-product-product_cat-trong-duong-dan-cua-woocommerce/ | |
ERROR CODE | |
DO NOT USE THIS CODE | |
*/ | |
function devvn_remove_slug( $post_link, $post ) { | |
if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) { | |
return $post_link; | |
} | |
if('product' == $post->post_type){ | |
$post_link = str_replace( '/product/', '/', $post_link ); //Thay product bằng slug hiện tại của bạn | |
}else{ | |
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); | |
} | |
return $post_link; | |
} | |
add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 ); | |
function devvn_parse_request( $query ) { | |
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { | |
return; | |
} | |
if ( ! empty( $query->query['name'] ) ) { | |
$query->set( 'post_type', array( 'post', 'product', 'page' ) ); | |
} | |
} | |
add_action( 'pre_get_posts', 'devvn_parse_request' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Remove product-category in URL | |
* Thay product-category bằng slug hiện tại của bạn. Mặc định là product-category | |
*/ | |
add_filter( 'term_link', 'devvn_product_cat_permalink', 10, 3 ); | |
function devvn_product_cat_permalink( $url, $term, $taxonomy ){ | |
switch ($taxonomy): | |
case 'product_cat': | |
$taxonomy_slug = 'product-category'; //Thay bằng slug hiện tại của bạn. Mặc định là product-category | |
if(strpos($url, $taxonomy_slug) === FALSE) break; | |
$url = str_replace('/' . $taxonomy_slug, '', $url); | |
break; | |
endswitch; | |
return $url; | |
} | |
// Add our custom product cat rewrite rules | |
function devvn_product_category_rewrite_rules($flash = false) { | |
$terms = get_terms( array( | |
'taxonomy' => 'product_cat', | |
'post_type' => 'product', | |
'hide_empty' => false, | |
)); | |
if($terms && !is_wp_error($terms)){ | |
$siteurl = esc_url(home_url('/')); | |
foreach ($terms as $term){ | |
$term_slug = $term->slug; | |
$baseterm = str_replace($siteurl,'',get_term_link($term->term_id,'product_cat')); | |
add_rewrite_rule($baseterm.'?$','index.php?product_cat='.$term_slug,'top'); | |
add_rewrite_rule($baseterm.'page/([0-9]{1,})/?$', 'index.php?product_cat='.$term_slug.'&paged=$matches[1]','top'); | |
add_rewrite_rule($baseterm.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat='.$term_slug.'&feed=$matches[1]','top'); | |
} | |
} | |
if ($flash == true) | |
flush_rewrite_rules(false); | |
} | |
add_action('init', 'devvn_product_category_rewrite_rules'); | |
/*Sửa lỗi khi tạo mới taxomony bị 404*/ | |
add_action( 'create_term', 'devvn_new_product_cat_edit_success', 10, 2 ); | |
function devvn_new_product_cat_edit_success( $term_id, $taxonomy ) { | |
devvn_product_category_rewrite_rules(true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Code Bỏ /product/ hoặc /cua-hang/ hoặc /shop/ ... có hỗ trợ dạng %product_cat% | |
* Thay /cua-hang/ bằng slug hiện tại của bạn | |
*/ | |
function devvn_remove_slug( $post_link, $post ) { | |
if ( !in_array( get_post_type($post), array( 'product' ) ) || 'publish' != $post->post_status ) { | |
return $post_link; | |
} | |
if('product' == $post->post_type){ | |
$post_link = str_replace( '/cua-hang/', '/', $post_link ); //Thay cua-hang bằng slug hiện tại của bạn | |
}else{ | |
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); | |
} | |
return $post_link; | |
} | |
add_filter( 'post_type_link', 'devvn_remove_slug', 10, 2 ); | |
/*Sửa lỗi 404 sau khi đã remove slug product hoặc cua-hang*/ | |
function devvn_woo_product_rewrite_rules($flash = false) { | |
global $wp_post_types, $wpdb; | |
$siteLink = esc_url(home_url('/')); | |
foreach ($wp_post_types as $type=>$custom_post) { | |
if($type == 'product'){ | |
if ($custom_post->_builtin == false) { | |
$querystr = "SELECT {$wpdb->posts}.post_name, {$wpdb->posts}.ID | |
FROM {$wpdb->posts} | |
WHERE {$wpdb->posts}.post_status = 'publish' | |
AND {$wpdb->posts}.post_type = '{$type}'"; | |
$posts = $wpdb->get_results($querystr, OBJECT); | |
foreach ($posts as $post) { | |
$current_slug = get_permalink($post->ID); | |
$base_product = str_replace($siteLink,'',$current_slug); | |
add_rewrite_rule($base_product.'?$', "index.php?{$custom_post->query_var}={$post->post_name}", 'top'); | |
add_rewrite_rule($base_product.'comment-page-([0-9]{1,})/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&cpage=$matches[1]', 'top'); | |
add_rewrite_rule($base_product.'(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?'.$custom_post->query_var.'='.$post->post_name.'&feed=$matches[1]','top'); | |
} | |
} | |
} | |
} | |
if ($flash == true) | |
flush_rewrite_rules(false); | |
} | |
add_action('init', 'devvn_woo_product_rewrite_rules'); | |
/*Fix lỗi khi tạo sản phẩm mới bị 404*/ | |
function devvn_woo_new_product_post_save($post_id){ | |
global $wp_post_types; | |
$post_type = get_post_type($post_id); | |
foreach ($wp_post_types as $type=>$custom_post) { | |
if ($custom_post->_builtin == false && $type == $post_type) { | |
devvn_woo_product_rewrite_rules(true); | |
} | |
} | |
} | |
add_action('wp_insert_post', 'devvn_woo_new_product_post_save'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@phongtran89 ah cái code kia chưa dc tối ưu lên mình chuyển sang dùng plugin. Cái chỗ này lưu code chơi vậy. sao bạn lại search vào đây ta :D