Last active
August 23, 2018 01:15
-
-
Save edmundcwm/d9156f557d12bfeff3b6205bc909bef0 to your computer and use it in GitHub Desktop.
Reduce length of Woocommerce product title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) . "…"; | |
} | |
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