Custom Product Tab Edited per Product
Adds an editor box to each product for an extra tab, such as "Ingredients" or "Specifications", that only appears on products where you filled it in.
Opens WPIDE on
Code
$wpide_field_tab = array(
'title' => 'Specifications', // Tab title.
'priority' => 25, // Position among the tabs.
);
add_action( 'add_meta_boxes_product', function () use ( $wpide_field_tab ) {
add_meta_box( 'wpide-tab-content', $wpide_field_tab['title'] . ' tab', function ( $post ) {
wp_nonce_field( 'wpide_tab_content', 'wpide_tab_content_nonce' );
wp_editor( get_post_meta( $post->ID, '_wpide_tab_content', true ), 'wpide_tab_content', array( 'textarea_rows' => 8, 'media_buttons' => false ) );
}, 'product', 'normal' );
} );
add_action( 'save_post_product', function ( $post_id ) {
if ( ! isset( $_POST['wpide_tab_content_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wpide_tab_content_nonce'] ), 'wpide_tab_content' ) || ! current_user_can( 'edit_post', $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
update_post_meta( $post_id, '_wpide_tab_content', wp_kses_post( wp_unslash( $_POST['wpide_tab_content'] ?? '' ) ) );
} );
add_filter( 'woocommerce_product_tabs', function ( $tabs ) use ( $wpide_field_tab ) {
global $product;
$content = $product instanceof WC_Product ? get_post_meta( $product->get_id(), '_wpide_tab_content', true ) : '';
if ( '' !== trim( (string) $content ) ) {
$tabs['wpide_field_tab'] = array(
'title' => $wpide_field_tab['title'],
'priority' => $wpide_field_tab['priority'],
'callback' => function () use ( $content ) {
echo wp_kses_post( wpautop( do_shortcode( $content ) ) );
},
);
}
return $tabs;
} );
Notes
Change the tab title in $wpide_field_tab; the content allows post HTML and runs shortcodes.
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.