Limit the Words in WooCommerce Product Short Description

WooCommerce has a filter that can limit the words in a product short description field  called ‘woocommerce_short_description‘ – coupled with an existing WordPress function ‘wp_trim_words‘ we can make the description any length to suit teasers, sliders etc and leave other areas where we want the full short description.

<?php // <~ don't add me in
add_filter( 'woocommerce_short_description', 'prefix_filter_woocommerce_short_description' );
/**
* Limit WooCommerce Short Description Field
*/
function prefix_filter_woocommerce_short_description( $post_post_excerpt ) {
// make filter magic happen here...
if(! is_product() ) { // add in conditionals
$text = $post_post_excerpt;
$words = 10; // change word length
$more = ' […]'; // add a more cta
$post_post_excerpt = wp_trim_words( $text, $words, $more );
}
return $post_post_excerpt;
};
view raw get-shorty.php hosted with ❤ by GitHub

In the code snippet above we are using a conditional to apply this to any usage of the short description outside of a single product post, the filter uses the 1 parameter $post_post_excerpt which is assigned to $text, the $word count is set to 10 with a […] used as the $more valuethese three parameters belong to wp_trim_words which is executed and returned back to $post_post_excerpt – which is now a 10 word product short description.

Nifty.

1 Comment

  1. buran on June 2, 2019 at 7:48 am

    thanks code work.

    How to put a limit on related products ?

Leave a Comment