Skip to content

Instantly share code, notes, and snippets.

@edmundcwm
Last active August 23, 2018 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edmundcwm/d9156f557d12bfeff3b6205bc909bef0 to your computer and use it in GitHub Desktop.
Save edmundcwm/d9156f557d12bfeff3b6205bc909bef0 to your computer and use it in GitHub Desktop.
Reduce length of Woocommerce product title
<?php
/**
* Reduce length of Woocommerce product title
*
* @param string $title product title
* @param int $id product id
* @return string modified product title
*
* Hook ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
* Snippet ref: https://firstsiteguide.com/wordpress-titles/
**/
function edm_reduce_product_title_length( $title, $id ) {
$prod = get_post_type( $id );
//Determines maximum length of title. Edit value accordingly
$max_length = 20;
//Reduce title length if current post type is a 'product' and if its title length is longer than $max_length
if ( $prod === 'product' && strlen( $title ) > $max_length ) {
return substr($title, 0, $max_length) . "&hellip;";
}
return $title;
}
add_filter( 'the_title', 'edm_reduce_product_title_length', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment