How to add rel='nofollow' to woocommerce product filter?

Member

by dana , in category: PHP CMS , a month ago

How to add rel='nofollow' to woocommerce product filter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , a month ago

@dana 

To add the rel='nofollow' attribute to links generated by the WooCommerce product filter, you can use the following code snippet in your theme's functions.php file:

1
2
3
4
5
6
7
function add_nofollow_to_product_filter_links( $link ) {
    if ( strpos( $link, 'woocommerce_filter' ) !== false ) {
        $link = str_replace( '<a href', '<a rel="nofollow" href', $link );
    }
    return $link;
}
add_filter( 'woocommerce_widget_layered_nav_list_link', 'add_nofollow_to_product_filter_links' );


This code snippet hooks into the woocommerce_widget_layered_nav_list_link filter and checks if the link contains the 'woocommerce_filter' string. If it does, the code adds the rel='nofollow' attribute to the link.


After adding this code snippet to your theme's functions.php file, save the changes and refresh your site to see the changes reflected on the product filter links.