Product Subtitle Field
Adds a subtitle field to products and shows it under the product name on product pages and in shop listings, for taglines like "Organic cotton, made in Portugal".
Opens WPIDE on
Code
$wpide_subtitle_in_loop = true; // Also show in shop listings.
add_action( 'woocommerce_product_options_general_product_data', function () {
global $product_object;
woocommerce_wp_text_input( array(
'id' => '_wpide_subtitle',
'label' => 'Subtitle',
'value' => $product_object instanceof WC_Product ? $product_object->get_meta( '_wpide_subtitle' ) : '',
) );
} );
add_action( 'woocommerce_admin_process_product_object', function ( $product ) {
// phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the product save nonce.
if ( isset( $_POST['_wpide_subtitle'] ) ) {
$product->update_meta_data( '_wpide_subtitle', sanitize_text_field( wp_unslash( $_POST['_wpide_subtitle'] ) ) );
}
} );
$wpide_subtitle_html = function ( $tag ) {
return function () use ( $tag ) {
global $product;
$subtitle = $product instanceof WC_Product ? $product->get_meta( '_wpide_subtitle' ) : '';
if ( '' !== $subtitle ) {
printf( '<%1$s class="wpide-subtitle">%2$s</%1$s>', tag_escape( $tag ), esc_html( $subtitle ) );
}
};
};
add_action( 'woocommerce_single_product_summary', $wpide_subtitle_html( 'p' ), 6 );
if ( $wpide_subtitle_in_loop ) {
add_action( 'woocommerce_after_shop_loop_item_title', $wpide_subtitle_html( 'span' ), 4 );
}
Notes
Set $wpide_subtitle_in_loop to false to show it on product pages only.
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.