Theo dõi Yestech trên goole news

Hướng dẫn remove slug product_tag hoặc product_cat woocommerce

web ga6bd6484b 1280

Hướng dẫn remove slug product_tag hoặc product_cat woocommerce

add_filter( 'term_link', 'custom_product_tag_taxonomy_link', 10, 3 );
function custom_product_tag_taxonomy_link( $termlink, $term, $taxonomy )
{
    if ( $taxonomy !== 'product_tag' )
        return $termlink;

    return str_replace( 'style/', '', $termlink );
}


add_filter( 'product_tag_rewrite_rules', 'product_tag_rewrite_rules_func' );
function product_tag_rewrite_rules_func( array $rules ): array {
    $terms = get_terms(
        [
            'taxonomy'   => 'product_tag',
            'hide_empty' => false,
        ]
    );
    $slugs         = wp_list_pluck( $terms, 'slug' );
    $slugs_pattern = '(' . implode( '|', array_unique( $slugs ) ) . ')';

    $new_rules = [];
    foreach ( $rules as $pattern => $query ) {
        $pattern               = str_replace( 'style/([^/]+)', $slugs_pattern, $pattern );
        $new_rules[ $pattern ] = $query;
    }
    return $new_rules;
}


function redirect_old_style_taxonomy_urls() {
    // Chỉ xử lý khi là taxonomy và URL chứa "/style/"
    if ( strpos( $_SERVER['REQUEST_URI'], '/style/' ) !== false ) {
        global $wp;

        $current_url = home_url( add_query_arg( array(), $wp->request ) );
        $new_url = str_replace('/style/', '/', $current_url);

        if ($current_url !== $new_url) {
            wp_redirect($new_url, 301);
            exit;
        }
    }
}
add_action('template_redirect', 'redirect_old_style_taxonomy_urls');

 

Tìm hiểu thêm  Hướng dẫn đa ngôn ngữ shipping title trong wordpress