Remove product category from slug - by Bas Kling

This topic has 2 replies, 2 voices, and was last updated 5 years, 1 months ago ago by Olga Barlow

  • Avatar: Bas_Kling
    Bas Kling
    Participant
    April 19, 2020 at 12:34

    Hi,

    I want to remove the product-category from the slug to improve SEO, and found this code:

    add_filter(‘request’, function( $vars ) {
    global $wpdb;
    if( ! empty( $vars[‘pagename’] ) || ! empty( $vars[‘category_name’] ) || ! empty( $vars[‘name’] ) || ! empty( $vars[‘attachment’] ) ) {
    $slug = ! empty( $vars[‘pagename’] ) ? $vars[‘pagename’] : ( ! empty( $vars[‘name’] ) ? $vars[‘name’] : ( !empty( $vars[‘category_name’] ) ? $vars[‘category_name’] : $vars[‘attachment’] ) );
    $exists = $wpdb->get_var( $wpdb->prepare( “SELECT t.term_id FROM $wpdb->terms t LEFT JOIN $wpdb->term_taxonomy tt ON tt.term_id = t.term_id WHERE tt.taxonomy = ‘product_cat’ AND t.slug = %s” ,array( $slug )));
    if( $exists ){
    $old_vars = $vars;
    $vars = array(‘product_cat’ => $slug );
    if ( !empty( $old_vars[‘paged’] ) || !empty( $old_vars[‘page’] ) )
    $vars[‘paged’] = ! empty( $old_vars[‘paged’] ) ? $old_vars[‘paged’] : $old_vars[‘page’];
    if ( !empty( $old_vars[‘orderby’] ) )
    $vars[‘orderby’] = $old_vars[‘orderby’];
    if ( !empty( $old_vars[‘order’] ) )
    $vars[‘order’] = $old_vars[‘order’];
    }
    }
    return $vars;
    });

    URL: https://wpdynamic.com/wordpress-developer/wordpress-code-snippets/how-to-remove-the-product-category-slug-in-wordpress-woocommerce/

    When I add this in functions.php in the child theme, will it still load when you guys provide an update for the master theme? How is custom work retained in general when dealing with child themes?

    1 Answer
    Avatar: Olga Barlow
    Olga Barlow
    Participant
    April 23, 2020 at 09:57

    Hello,

    Does this code work with the default WooCommerce theme – Storefront? Additional customization is not included in our basic support. If you need help with additional customization you can submit customization request here.

    Regards

  • Viewing 2 results - 1 through 2 (of 2 total)

The issue related to '‘Remove product category from slug’' has been successfully resolved, and the topic is now closed for further responses

Helpful Topics

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.