Bước 1: Set chuyển hướng trong seo plugin rankmath hoặc

// sua link tag
function custom_query_vars($vars) {
$vars[] = 'tag_id';
return $vars;
}
add_filter('query_vars', 'custom_query_vars');
function custom_tag_rewrite() {
add_rewrite_rule(
'^tag/([^/]+)-([0-9]+)\.html?$',
'index.php?tag_id=$matches[2]&tag=$matches[1]',
'top'
);
}
add_action('init', 'custom_tag_rewrite');
function custom_pre_get_posts($query) {
// Chỉ áp dụng cho frontend, main query và khi có tag_id
if ( !is_admin() && $query->is_main_query() && get_query_var('tag_id') ) {
$term_id = get_query_var('tag_id');
$term = get_term($term_id, 'post_tag');
if ( $term && !is_wp_error($term) ) {
// Override query var 'tag' bằng slug chính xác của tag
$query->set('tag', $term->slug);
// Đảm bảo đây là tag archive
$query->is_tag = true;
}
}
}
add_action('pre_get_posts', 'custom_pre_get_posts');
add_filter('term_link', 'custom_tag_link', 10, 3);
function custom_tag_link($url, $term, $taxonomy) {
if ($taxonomy === 'post_tag') {
$url = home_url('tag/' . $term->slug . '-' . $term->term_id . '.html');
}
return $url;
}
// sua link san pham
function lbk_product_rewrite()
{
$wc_options = get_option('woocommerce_permalinks');
$product_product_base = $wc_options['product_base'];
global $wp_rewrite;
$wp_rewrite->extra_permastructs['product']['struct'] = $product_product_base . '/%product%-%post_id%.html';
flush_rewrite_rules();
}
add_action('init', 'lbk_product_rewrite');
add_filter('post_type_link', function ($post_link, $post) {
if ($post && 'product' === $post->post_type) {
return str_replace('%post_id%', $post->ID, $post_link);
}
return $post_link;
}, 10, 2);
