WooCommerce PHP Intermediate Requires WooCommerce

Add a Custom Product Tab

Add your own tab to every product page, such as Shipping & Returns, with content you write once.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 16 lines
add_filter( 'woocommerce_product_tabs', function ( array $tabs ): array {
	$tabs['nahnu_shipping'] = [
		'title'    => 'Shipping & returns',
		'priority' => 25,
		'callback' => 'nahnu_shipping_tab_content',
	];
	return $tabs;
} );

function nahnu_shipping_tab_content(): void {
	?>
	<h2>Shipping & returns</h2>
	<p>Orders ship within 2 business days. Free returns within 30 days of delivery.</p>
	<p><a href="<?php echo esc_url( home_url( '/shipping/' ) ); ?>">Read the full policy</a></p>
	<?php
}

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Shipping and returns, sizing and care instructions are the same for most products. Rather than repeating them in every description, put them in a tab that appears on every product page.

How it works

The woocommerce_product_tabs filter accepts new tabs. Each needs a unique key, a title, a priority (WooCommerce uses 10, 20 and 30 for its own tabs, so 25 places this one between Additional information and Reviews) and a callback function that prints the content.

The callback runs only when the tab is shown, so the content is always current.

Things to watch

  • Keep the text in the callback simple. To let non-developers edit it, read it from a page with get_post_field( 'post_content', $page_id ) instead.
  • To show the tab only for some products, check has_term() or global $product inside the filter before adding it.

How to test it

Open a product page. A “Shipping & returns” tab should appear between the default tabs.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only