Adding Meta Description to WordPress pages without a plugin

Age of Article Warning:
This article was originally published 68 months ago. The information, tips and techniques explained may outdated. Examples shown on this page may no longer work. Please consider this when viewing the below content.

add meta description to wordpress header

How to add meta descriptions to certain posts without a plugin?

Firstly I’d recommend trying an SEO plugin like All in One SEO to handle your search engine optimisation. However sometimes you might have a need to manually add a meta description onto a page.

Or you may want to do something different on a particular post or page, in which case you could turn off the SEO plugin for that page and then add some custom code to your header.php file that only inserts it onto the applicable page(s).

Updates:

update 14/4/2013
The original code for this did not take into consideration that you cannot use “the_excerpt” outside of the loop. The code below has been adjusted for this and instead of using “the_excerpt” it now uses “the_title”. If you prefer to use “the_excerpt” in your header code, then there is some code further down this page that allows for using this outside the loop.

For this exercise we are now going to use “the_title” and insert that into the header of your page, into the meta description. Paste the following code into your header.php file.

<?php if (is_single() && in_category('1')) { ?>
<meta name="description" content="<?php the_title();?>"/>
<?php } ?> 

So this would identify if the page is a single post and in category 1. If yes, then it adds meta description based on the_title.

If you want to choose a range of posts, say with IDs 41,48,103 and 152, then you could use:

<?php if is_single(array( 41,48,103,152 )) { ?>
<meta name="description" content="<?php the_title();?>"/>
<?php } ?> 

or if you wanted to add the category description to the meta description of a category page, you could try:

<?php if (is_category()) { 
$cat = get_query_var('cat'); 
$metacat= strip_tags(category_description($cat));?>
<meta name="description" content="<?php echo $metacat;?>"/>
<?php } ?> 

You can also set meta description based on “the_excerpt” using the following technique, which allows for calling “get_the_excerpt” outside of the loop.

<?php if (is_single() || is_page() ) : if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<meta name="description" content="<?php echo get_the_excerpt();?>">
<?php endwhile; endif; elseif (is_home() ): ?>
<meta name="description" content="<?php bloginfo('description'); ?>- AND ADD SOME MORE CONTENT HERE">
<?php endif; ?>

This code will add the excerpt to the meta description on your posts and pages. Additionally it allows for you to further customise the meta description of your home page, by adding your bloginfo description as well as extra content for enhancing your SEO.

30 thoughts on “Adding Meta Description to WordPress pages without a plugin

  1. Thank your for posting these code tips, I used the one related to category description as meta description and it worked just fine. Cheers from Brazil.

    • Hi Naomi from Brazil, thanks for the great feedback. Hope you find some more great tips over the coming weeks. You’re welcome to signup to my email newsletter to stay in the loop with a weekly summary of posts, or just keep checking back whenever you feel like it. Happy to have helped!

    • Hi there, if you have implemented the code into the header.php file of your WordPress childtheme, then be sure to view the source code of that page and see what output is occurring.

    • Hi Ashu, I think that the “|” pipe character is often used in page titles, so I imagine it can also be used in meta descriptions

    • Putting this in your functions.php file will take the title and split it by the ‘ – ‘ characters (note it is a space then a hyphen followed by a space).
      [code]function get_split_title($postID) {
      $title = get_the_title($postID);
      $lines = explode(‘ – ‘, $title);
      $output = false;
      $count = 0;

      foreach( $lines as $line ) {
      $count++;
      $output .= ‘<span class="line-‘.$count.’">’.$line.'</span> ‘;
      }

      return $output;
      }[/code]
      And then just use [code]<?php echo get_split_title($post->ID); ?>[/code] to recall it,hope it will help u

    • Gyrate360,
      looks like that code could be used to add the title of the page into a function that you could output onto a page, or into the header is required.
      You probably also could use echo get_the_title($post->ID); into the header

  2. hi David, if possible how to add meta desc, meta keywords, meta robots for home (from bloginfo description), category(category_description) and page(the_content). any ideas,
    thanks

  3. You should have titled this: How to use “the_excerpt” outside of THE LOOP by using THE LOOP… Haha. I was stumped at first how to do this myself, until I realized that you can loop through posts more than once for each page! Great write up!

  4. Thanks for sharing this wonderful post with us.hope to learn more from you.The best thing about your post which i like is,that you presented in a such a way that it can be easily understandable,Thanks

  5. Hi David,
    yep its me again with another wordpress.com question. could I add this code into the header part of my posts? Google Website Tools always flags my blog for short meta descriptions.

    -thanks Robert

    • Hi Robert,

      Yes you are probably correct, I don’t know that you have access to the theme files to amend them to make these changes. Perhaps you could look at supported plugins for WordPress.com sites to see if anything there can be used to help achieve your needs http://en.support.wordpress.com/plugins/

      good luck, David

Comments are closed.