Adding Meta Description to WordPress pages without a plugin

Age of Article Warning:
This article was originally published 62 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).


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.

Filed under: SEO and Marketing, WordPressTagged with: ,


  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!

  2. <meta name="description" content="”> not showing 🙁

    • 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.

  3. can i add a meta description using symbol “|” between words?

    • 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 ) {
      $output .= ‘<span class="line-‘.$count.’">’.$line.'</span> ‘;

      return $output;
      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

  4. But I just add the site meta description in the index.php template,it also works well

    • Hi Gyrate360
      yes you can just add the applicable code into the page templates.

  5. 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,

    • Hi Dhoi,

      not sure what you are trying to do exactly, however you can add blog info description to your home page header by including the following in the head section:

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

      Meta robots is talked about here:

      Meta keywords are not used by Google for indexing, however if you want to include in your home page, then include:

      < meta name="keywords" content="LIST KEYWORDS HERE" >

      hope this helps,

  6. 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!

    • Hi Israel,
      thanks for the thoughts here,

  7. hi David,
    thanks for your helpful post

    • Hi Mahdi,
      glad to have helped

  8. I want to let my users post on my site in near future but I don’t want them to get confused with SEO titles and meta descriptions etc. So thanks for your help here.

    • Veli, it’s good to keep things simple when you can

  9. 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

    • happy to have helped Tafoor

  10. Hi David,
    yep its me again with another 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 sites to see if anything there can be used to help achieve your needs

      good luck, David

  11. The author thanks BRO, this is exactly what I was looking for

    • Excellent, glad to have helped out then 🙂

  12. Nice works David
    how to put meta keyword, description and title using your tutorial
    i search everywhere but didn’t find
    do you mind shared your tutorial

  13. Thanks for sharing this wonderful post with us.hope to learn more from you

    • Glad you found it useful

Comments are closed for this article!