Though most search engine does not take meta description tag into consideration when deciding where to rank a web page, but Search Engine Optimization (SEO) experts believe otherwise. Nevertheless, meta description tag on the header does been used for displaying as content snippet in search engine results page (SERP).

WordPress does not automatically include a meta description into the header section of the web page. Even if there are themes that have description meta tag support, it is usually contains description value which is the same across the whole website. There are many plugins that do the job to insert a description meta tag based on either except or first few sentences of the article. These plugins mainly is meant for overall SEO to optimize a WordPress website for better ranking in search engine.

If you’re not interested in installing any plugin just to get the description meta tag, use the following PHP script code to get WordPress to automatically generate and insert a description meta tag for each posts and/or pages by using the first few words of the content up to a specified number of characters, without any plugins.

Put the following code into functions.php of the WordPress theme in active:

function gen_meta_desc() {
   global $post;

   // remove is_page() to put description meta on posts only
   if ( (is_single()) || (is_page()) ) { 
      // specify the length of content snippet to put in meta description
      $length = 150;
      // strip HTML tags
      $meta = strip_tags($post->post_content);
      // strip shortcodes
      $meta = strip_shortcodes($meta);
      // replace newline indicator with space
      $meta = str_replace(array("\n", "\r", "\t"), " ", $meta);
      // truncate the description based length specified, up to the last full word 
      $meta = preg_replace('/\s+?(\S+)?$/', '', substr($meta, 0, $length));
   } else {
      if ( (is_category()) || (is_tag()) ) {
         if (is_category()) {
            $meta = "Posts related to Category: " . single_cat_title("", FALSE);
         if (is_tag()) {
            $meta = "Posts related to Tag: " . single_tag_title("", FALSE);

   echo ">meta name='description' content='$meta' />";

// automatically insert description meta tag via wp_head() function
add_action('wp_head', 'gen_meta_desc');

It’s also possible to put the whole function above directly into theme’s header.php, between the <head> and </head> section where you want to meta to appear. Or, you can put the function in functions.php file like above, and then call the function manually by specifying <?php gen_meta_desc(); ?> between the <head> and </head> section where you want to meta to appear.

Note that the add_action function depends on availability of wp_head() in the header.php of the theme. If the wp_head() is not called, you have to manually call the function to insert the description meta tag.

An alternative is to call the WordPress loop to grab the content excerpt. The example below use the method which directly placed inside the header.

<meta name="description" content="<?php 
if ( have_posts() && (is_single() || is_page()) ) : while(have_posts()) : the_post(); 
   $meta = str_replace(array("\n", "\r", "\t"), " ", get_the_excerpt());
   echo apply_filters('the_excerpt_rss', $meta);
elseif ( is_category() || is_tag() ) :
   if ( is_category() ) :
      echo "Posts related to Category: " . single_cat_title("", FALSE);
   elseif ( is_tag() ) :
      echo "Posts related to Tag: " . single_tag_title("", FALSE);
   echo "Write your own meta description for homepage and other pages"; ?>
<?php endif; ?>" />