Automatic Meta Description Tags Using Excerpts

The default installation of WordPress does not contain the description and keywords meta tag data. Meta tags can be added manually, through changes to the Theme template files, WordPress Plugins or by adding code to your theme’s functions.php file.

What Are Meta Tags?

The word meta means “information about”. Meta Tags were created early on to provide concise information about a website. Meta tags list information about the web page, such as the author, keywords, description, type of document, copyright, and other core information.

Meta tags provide some information for a Web browser so the page can be rendered properly. Example:

<meta http-equiv="content-language" content="en-us" /> Language
<meta name="author" content="Roger Kaputnik" /> Author of page
<meta name="description" content="How to make beer in your bathtub the same way grandpa did during Prohibition." /> Description of page
<meta name="keywords" content="beer, ale, hops, bottles, homemade, hobby" /> Keywords used on page

At one time, search engines relied heavily upon the Description and Keywords tags to determine a page’s relevance and placement in search results. Google no longer uses the meta keyword in search result ranking.

“Google will sometimes use the Meta Description tag from a page to generate a search results snippet, if we think it gives users a more accurate description than would be possible purely from the on-page content. A meta description tag should generally inform and interest users with a short, relevant summary of what a particular page is about.” Google source

Add a Meta Description Tag to Your Posts and Pages

The simplest method of adding a Meta Description tag to your posts and pages is to set a site Tagline in the Dashboard:
Settings > General > Tagline. Then add this code to your theme’s header.php file:

<meta name="description" content="<?php bloginfo('description'); ?>" />

The downside of this method is that all of your posts and pages will have the same description, the Tagline you set.

Unique Meta Description Tags Without a Plugin

To have a unique Meta Description tag for each of your posts and pages you will need to use a plugin or modify your theme’s functions.php file. Meta Description plugins can be found at wordpress.org.

If you don’t want to use a plugin add the code below to your theme’s functions.php file. What it does:

  • Uses the Excerpt for the Meta Description of Posts/Pages.
  • Removes HTML tags from the post Excerpt.
  • Uses 35 words for the Meta Description. (can be modified)
  • Automatically adds the Meta Description to the page header using:
    • the Excerpt for posts and pages
    • or the Category Description for archive pages.

Example: Post or Page Meta Description

Example: Archive Page Meta Description

Add to theme’s functions.php file:

// add an excerpt box when composing or editing pages
add_post_type_support( 'page', 'excerpt' );

// customize the excerpt for use in meta description tags
function atconet_get_excerpt($post_id) {
	$temp = $post;
	$post = get_post( $post_id );
	setup_postdata( $post );
	$excerpt = get_the_excerpt();
	$description = implode( ' ', array_slice( explode( ' ', $excerpt), 0, 35 ) ); // # of words in Meta Description
	wp_reset_postdata();
	$post = $temp;
return $description;
}

// add meta description tag to page header
function atconet_add_meta_description_tag() { ?>
	<meta name="description" content="<?php if ( is_single() || is_page() ) { // for posts & pages
		$description = atconet_get_excerpt( get_the_ID() );
		echo $description;
	}
	if ( is_archive() ) { // use the Category Description for the Meta Description
		echo wp_strip_all_tags( term_description() );
	} ?>
	" />
<?php 
}
add_action( 'wp_head', 'atconet_add_meta_description_tag', 1 );