Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active March 10, 2020 18:25
Show Gist options
  • Save morgyface/02e8dd0cf72efddd621ae519cbd6ab4b to your computer and use it in GitHub Desktop.
Save morgyface/02e8dd0cf72efddd621ae519cbd6ab4b to your computer and use it in GitHub Desktop.
WordPress | Function | Use post/page excerpt to generate meta description for SEO
<?php
// Generating a meta description using standard WP components
function meta_description( $char_limit ) {
$tagline = get_bloginfo ( 'description' );
$post_object = get_post();
$excerpt = $post_object->post_excerpt; // Get the raw excerpt, warts (tags) and all.
$content = $post_object->post_content; // Get the raw content.
if ( !empty( $excerpt ) ) { // If there is an excerpt lets use it to generate a meta description
$excerpt_stripped = strip_tags( $excerpt ); // Remove any tags using the PHP function strip_tags.
$excerpt_length = strlen( $excerpt_stripped ); // Now lets count the characters
if ( $excerpt_length > $char_limit ) { // Now work out if we need to trim the character length.
$offset = $char_limit - $excerpt_length; // This gives us a negative value.
$position = strrpos( $excerpt_stripped, ' ', $offset ); // This starts looking for a space backwards from the offset
$description = substr( $excerpt_stripped, 0, $position ); // Trim up until the point of the last space.
} else {
$description = $excerpt_stripped; // The excerpt must be less than the char_limit so lets just print it.
}
} elseif( !empty( $content ) ) {
// If no excerpt exists we use the content, note the use of get_post as we are outside the loop.
$content_stripped = strip_tags( $content );
$content_length = strlen( $content_stripped );
if ( $content_length > $char_limit ) {
$content_trimmed = substr( $content_stripped, 0, $char_limit ); // Trim the raw content back to the character limit
$last_space = strrpos( $content_trimmed, ' ' ); // Find the last space in the trimmed content which could include incomplete words
$description = substr( $content_trimmed, 0, $last_space ); // Re-trim the content to the point of the last space.
} else {
$description = $content_stripped;
}
} else {
$description = $tagline; // If the page is empty we can use the tagline to prevent emptiness.
}
return $description;
}
?>
@morgyface
Copy link
Author

morgyface commented Jan 30, 2018

Meta description from page content

To use, add this code to your theme's functions.php file and then echo meta_description(155) in your theme's header file.

Use like this in a normal meta tag: <?php echo '<meta name="description" content="' . meta_description(155) . '">' ; ?>

Or like this as HTML: <meta name="description" content="<?php echo meta_description(155); ?>">

Parameter

$char_limit

(integer) (Required) How long you want the description to be.
Note: At the time of writing this it would appear 155 is a good length for SEO purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment