Trong bài viết này, chúng ta sẽ tìm hiểu cách sử dụng đoạn code WordPress để tùy chỉnh URL của danh mục sản phẩm (product_cat) và bài viết sản phẩm (product) trong WooCommerce, giúp tạo ra các URL thân thiện với SEO bằng cách thêm hậu tố .html. Code này sử dụng các bộ lọc (filters) và quy tắc viết lại (rewrite rules) để định dạng URL theo cấu trúc mong muốn.
Mục đích của đoạn code
Đoạn code này giúp:
-
Tùy chỉnh URL của danh mục sản phẩm thành dạng tên-danh-mục.html.
-
Tùy chỉnh URL của sản phẩm thành dạng tên-sản-phẩm.html.
-
Tự động cập nhật quy tắc viết lại khi thêm, sửa hoặc xóa danh mục/sản phẩm.
-
Hỗ trợ phân trang và feed RSS cho các URL đã tùy chỉnh.
Cách sử dụng code
1. Thêm code vào file functions.php
Sao chép toàn bộ đoạn code và thêm vào file functions.php của theme WordPress đang sử dụng (hoặc child theme để tránh mất code khi cập nhật theme):
add_filter('term_link', function($url, $term, $taxonomy){ if ($taxonomy !== 'product_cat') return $url; return home_url('/' . $term->slug . '.html'); }, 10, 3);
add_filter('post_type_link', function($permalink, $post, $leavename, $sample){ if ($post->post_type !== 'product') return $permalink; return home_url('/' . $post->post_name . '.html'); }, 10, 4);
function md_build_product_cat_rules($flush = false){ $terms = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => false, 'fields' => 'all', ]); if (!is_wp_error($terms) && $terms){ foreach ($terms as $t){ $slug = sanitize_title($t->slug); $q = preg_quote($slug, '#'); add_rewrite_rule('^' . $q . '\.html/?$', 'index.php?product_cat=' . $slug, 'top'); add_rewrite_rule('^' . $q . '\.html/page/([0-9]{1,})/?$', 'index.php?product_cat=' . $slug . '&paged=$matches[1]', 'top'); add_rewrite_rule('^' . $q . '\.html/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product_cat=' . $slug . '&feed=$matches[1]', 'top'); } } if ($flush) flush_rewrite_rules(false); }
function md_build_product_rules($flush = false){ $ids = get_posts([ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', ]); foreach ((array)$ids as $pid){ $slug = get_post_field('post_name', $pid); if (!$slug) continue; $q = preg_quote($slug, '#'); add_rewrite_rule('^' . $q . '\.html/?$', 'index.php?product=' . $slug, 'top'); add_rewrite_rule('^' . $q . '\.html/comment-page-([0-9]{1,})/?$', 'index.php?product=' . $slug . '&cpage=$matches[1]', 'top'); add_rewrite_rule('^' . $q . '\.html/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?product=' . $slug . '&feed=$matches[1]', 'top'); } if ($flush) flush_rewrite_rules(false); }
add_action('init', function(){ md_build_product_cat_rules(false); md_build_product_rules(false); }, 20);
add_action('created_term', function($term_id, $tt_id, $taxonomy){ if ($taxonomy === 'product_cat') md_build_product_cat_rules(true); }, 10, 3);
add_action('edited_term', function($term_id, $tt_id, $taxonomy){ if ($taxonomy === 'product_cat') md_build_product_cat_rules(true); }, 10, 3);
add_action('delete_term', function($term_id, $tt_id, $taxonomy){ if ($taxonomy === 'product_cat') md_build_product_cat_rules(true); }, 10, 3);
add_action('save_post_product', function($post_id){ if (wp_is_post_revision($post_id)) return; md_build_product_rules(true); }, 10, 1);
add_action('deleted_post', function($post_id){ if (get_post_type($post_id) === 'product') md_build_product_rules(true); }, 10, 1);
2. Cập nhật lại đường dẫn tĩnh
-
Sau khi thêm code, vào Cài đặt > Đường dẫn tĩnh trong admin WordPress và lưu lại để làm mới bộ quy tắc viết lại.
-
Kiểm tra các URL danh mục sản phẩm và sản phẩm để đảm bảo chúng hiển thị đúng dạng ten-danh-muc.html và ten-san-pham.html.
Đoạn code này là một giải pháp mạnh mẽ để tối ưu hóa URL sản phẩm và danh mục sản phẩm trong WooCommerce. Nó không chỉ giúp URL thân thiện hơn với SEO mà còn đảm bảo tính linh hoạt khi quản lý nội dung. Nếu bạn cần hỗ trợ thêm hoặc muốn tùy chỉnh code, hãy liên hệ với Nguyễn Lân