Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.