WooCommerce’s Gutenberg blocks for product excerpts do not allow HTML tags by default, even though the product description in the backend can include HTML. To overcome this limitation, I created a custom block for the product excerpt. Here’s the process I followed and the necessary code.
Step-by-Step Solution
- Enqueue Block Editor Assets: Add the following PHP code to your theme’s
functions.phpfile to enqueue the block editor script.
function mytheme_enqueue_block_editor_assets() {
// Enqueue block editor script
wp_enqueue_script(
'mytheme-block-editor-js', // Handle
get_theme_file_uri('/build/index.js'), // Path to the JS file
array( 'wp-blocks', 'wp-i18n', 'wp-element' ), // Dependencies
filemtime(get_theme_file_path('/build/index.js')) // Version: file modification time for cache busting
);
}
add_action('enqueue_block_editor_assets', 'mytheme_enqueue_block_editor_assets');
- Render the Custom Block: Define the PHP function to render the custom block on the front end. Add this code to your
functions.phpfile.
function render_my_post_excerpt_block($attributes, $content) {
$post_id = $attributes['postId'];
$post = get_post($post_id);
$excerpt = apply_filters('the_excerpt', $post->post_excerpt);
return '<div>' . $excerpt . '</div>';
}
register_block_type('my-plugin/my-post-excerpt', [
'render_callback' => 'render_my_post_excerpt_block',
]);
- Create the Block Editor Script: Save the following JavaScript code in
index.jswithin thebuilddirectory of your theme.
// Import dependencies
const { registerBlockType } = wp.blocks;
const { __ } = wp.i18n;
const { useSelect } = wp.data;
const { useEffect, useState } = wp.element;
registerBlockType('my-plugin/my-post-excerpt', {
title: __('Product Excerpt', 'my-plugin'),
icon: 'smiley',
category: 'common',
usesContext: ['postId'],
attributes: {
excerpt: {
type: 'string',
source: 'html',
selector: 'p'
}
},
edit: ({ attributes, setAttributes, context }) => {
const { postId } = context;
const { excerpt } = attributes;
const [badges, setBadges] = useState([]);
useEffect(() => {
let isMounted = true;
if (postId) {
wp.data.resolveSelect('core').getEntityRecord('postType', 'product', postId).then(product => {
if (product && product.excerpt && isMounted) {
setAttributes({ excerpt: product.excerpt.rendered });
}
}).catch(error => {
console.error('Error fetching product:', error);
});
}
return () => {
isMounted = false;
};
}, [postId]);
return (
<div>
<div dangerouslySetInnerHTML={{ __html: excerpt || __('No excerpt', 'my-plugin') }} />
<ul>
{badges.map((badge, index) => (
<li key={index}>{badge}</li>
))}
</ul>
</div>
);
},
save: () => null // Render in PHP
});
By following these steps, you will be able to create a custom block that allows HTML in WooCommerce product excerpts. This custom block can be used to display excerpts with HTML tags on the front end, providing more flexibility in how product information is presented.


This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks. The filter approach is much cleaner than editing core files.
Glad it helped! Using filters is always the recommended approach since it survives WooCommerce updates.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x and want to make sure before implementing.
Yes, this approach uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Already added it to my child theme’s functions.php and it works perfectly.
That’s the way to go! Simple snippets are always better than loading a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions. This solved it.
You’re welcome! Product excerpts look much more professional with proper HTML formatting.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks. The filter approach is much cleaner than editing core files.
Glad it helped! Using filters is always the recommended approach since it survives WooCommerce updates.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x and want to make sure before implementing.
Yes, this approach uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Already added it to my child theme’s functions.php and it works perfectly.
That’s the way to go! Simple snippets are always better than loading a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions. This solved it.
You’re welcome! Product excerpts look much more professional with proper HTML formatting.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks. The filter approach is much cleaner than editing core files.
Glad it helped! Using filters is always the recommended approach since it survives WooCommerce updates.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x and want to make sure before implementing.
Yes, this approach uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Already added it to my child theme’s functions.php and it works perfectly.
That’s the way to go! Simple snippets are always better than loading a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions. This solved it.
You’re welcome! Product excerpts look much more professional with proper HTML formatting.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks. The filter approach is much cleaner than editing core files.
Glad it helped! Using filters is always the recommended approach since it survives WooCommerce updates.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
Does this also apply to variable product descriptions or just simple products?
It works for all product types since it hooks into the core excerpt filter.
Clean and elegant solution. I was about to hack the core template files before finding this.
Always avoid modifying core files! Filters are the WordPress way to handle these customizations.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
Does this also apply to variable product descriptions or just simple products?
It works for all product types since it hooks into the core excerpt filter.
Clean and elegant solution. I was about to hack the core template files before finding this.
Always avoid modifying core files! Filters are the WordPress way to handle these customizations.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
Does this also apply to variable product descriptions or just simple products?
It works for all product types since it hooks into the core excerpt filter.
Clean and elegant solution. I was about to hack the core template files before finding this.
Always avoid modifying core files! Filters are the WordPress way to handle these customizations.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
Does this also apply to variable product descriptions or just simple products?
It works for all product types since it hooks into the core excerpt filter.
Clean and elegant solution. I was about to hack the core template files before finding this.
Always avoid modifying core files! Filters are the WordPress way to handle these customizations.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.
Quick question — does this work with the latest WooCommerce version? I’m running 8.x.
Yes, this uses WordPress core filters, so it’s compatible with all WooCommerce versions.
I was using a plugin for this but your code snippet is much lighter. Works perfectly.
Simple snippets are always better than a full plugin for one small feature.
Thank you! I’ve been looking for a way to add styled bullet points in my product short descriptions.
You’re welcome! Proper HTML formatting makes products look much more professional.
Finally a solution that doesn’t break on WooCommerce updates. Added to my child theme’s functions.php.
That’s the safest approach! Child theme modifications always survive updates.
Does this also apply to variable product descriptions or just simple products?
It works for all product types since it hooks into the core excerpt filter.
This was exactly what I needed! I was struggling with HTML being stripped from my product excerpts for weeks.
Glad it helped! Using filters is always the recommended approach.