Skip to content

Instantly share code, notes, and snippets.

@schemapress
Created September 24, 2017 21:56
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 schemapress/560cd6255c6a5974e51172664b161d17 to your computer and use it in GitHub Desktop.
Save schemapress/560cd6255c6a5974e51172664b161d17 to your computer and use it in GitHub Desktop.
Shorten and Limit Headline by 110 characters for Schema plugin https://schema.press/
<?php //* do not include php tag
add_filter('schema_output', 'schema_wp_shorten_headline_output_98734524456');
/**
* Shorten / Limit Headline in Schema Output
*
* String length of headline must be in range [0, 110]
*
* @since 1.0
*/
function schema_wp_shorten_headline_output_98734524456( $schema ) {
global $post;
if ( empty($schema) ) return;
$more = '…'; // indicate that it was shortened
// shorten to a number of words
// $num_words = 12; // change to your liking
// $schema['headline'] = wp_trim_words( $schema['headline'], $num_words, $more );
// shorten to a number of characters
// $num_characters = 110; // change to your liking
// $schema['headline'] = substr( $schema['headline'], 0, $num_characters );
// or...
$num_characters = 109; // change to your liking, recommended is 109
$schema['headline'] = rtrim( mb_strimwidth( $schema['headline'], 0, $num_characters ) ) . $more;
return $schema;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment