Show ‘reviews’ tab as the first tab among ‘description’ and ‘additional information’ tabs on a WooCommerce website to give more visibility to reviews. We have already seen the different approach for more reviews visibility by showing reviews separately out of the tabs section. Now, let move the ‘reviews’ tab position in the tabs section on WooCommerce product pages.
Preview: Before and After
PHP Snippets: Move Reviews Tab
In the WordPress Dashboard, go to Appearance > Theme Editor and open the functions.php file. Add the following code at the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
add_filter( 'woocommerce_product_tabs', 'wpd_wc_move_product_review_tab', 1005 ); /** * Move Reviews tab before Description tab * * @param array $tabs Array of tabs. * @return array */ function wpd_wc_move_product_review_tab( $tabs ) { global $product, $post; $new_tabs = array(); if ( comments_open() ) { $new_tabs['reviews'] = $tabs['reviews']; unset( $tabs['reviews'] ); if ( $post->post_content ) { $new_tabs['description'] = $tabs['description']; unset( $tabs['description'] ); } if ( $product && ( $product->has_attributes() || apply_filters( 'wc_product_enable_dimensions_display', $product->has_weight() || $product->has_dimensions() ) ) ) { $new_tabs['additional_information'] = $tabs['additional_information']; unset( $tabs['additional_information'] ); } $tabs = array_merge( $new_tabs, $tabs ); } return $tabs; } |
The Code Explanation
You can filter (add/edit/delete) the existing tabs with the woocommerce_product_tabs filter. First, I am creating a new tabs array variable and then checking if reviews form is open for a product. If reviews form is open, then we are re-positioning specific tabs.
- 14-15: Re-assigning the existing reviews tab at first place in new tabs array and removing the existing reviews tab from the old tabs array.
- 17-20: If a product has Description tab, we assign the old description tabs at 2nd place in the new tabs array and removing it from the old tabs array.
- 22-25: If a product has Additional Information tab, we assign this tab the 3rd place in the new tabs array and remove it from the old tabs array.
- 27: Merging two arrays and creating a new array.
- 30: Returning array for the display.
Zishan Amin says
Helped me as well.
hassan says
really thanks for this