Shared Product Tab for One Category
Adds the same extra tab, such as "Care instructions" for clothing or "Allergens" for food, to every product in the categories you choose.
Opens WPIDE on
Code
$wpide_cat_tab = array(
'categories' => array( 'clothing' ), // Category slugs.
'title' => 'Care instructions',
'content' => '<p>Machine wash at 30°C. Do not tumble dry. Iron on a low heat inside out.</p>',
'priority' => 30,
);
add_filter( 'woocommerce_product_tabs', function ( $tabs ) use ( $wpide_cat_tab ) {
global $product;
if ( $product instanceof WC_Product && has_term( $wpide_cat_tab['categories'], 'product_cat', $product->get_id() ) ) {
$tabs['wpide_cat_tab'] = array(
'title' => $wpide_cat_tab['title'],
'priority' => $wpide_cat_tab['priority'],
'callback' => function () use ( $wpide_cat_tab ) {
echo wp_kses_post( do_shortcode( $wpide_cat_tab['content'] ) );
},
);
}
return $tabs;
} );
Notes
Edit the content here once and every product in the categories updates; shortcodes are run.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.