Meta description is an important factor for Search Engine Optimization (SEO). Search engines use it for display snippet in the search results. Via meta description, visitors can see a brief and concise summary of your page’s content. In WordPress, if you use SEO plugins like All-In-One SEO, it already has the built-in feature for auto generation meta description tag. But you can do it yourself without any plugins, and here it is!

Meta Description Tag
How meta description appears in search results

The idea of generating meta description is getting the first paragraph of your post and remove all formats, truncate it to a appropriated length so search engines can understand and display it correctly.

To do that, simply paste the following code into your functions.php file of your theme:

add_action( 'wp_head', 'prefix_meta_desc' );
function prefix_meta_desc() {
    if ( ! is_singular() )

    $meta = strip_tags( get_post()->post_content );
    $meta = str_replace( array( "\\n", "\\r", "\\t" ), ' ', $meta);
    $meta = substr( $meta, 0, 155 );

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

The code above will run only in singular pages (pages, single posts and all single custom post types). It removes all HTML tags from the post content, replaces all special whitespace characters (tabs, new lines) to a single space and truncates it to 155 characters.

Be sure that you added wp_head() in your theme header.php file.

Why should meta description have 155 characters?

According to the study of MOZ, the appropriate length of meta description is around 155 characters. The search engines don’t recommend an exact number because it depends on which language of your website and the length of the words.

Meta descriptions can be any length, but search engines generally truncate snippets longer than 160 characters. It is best to keep meta descriptions between 150 and 160 characters.

Now you can forget the SEO plugins and makes your theme optimized for SEO. If you find anything interesting about SEO or meta description tag, please let me know in the comments.


  1. wow..thanks Iike your share source code this auto generate meta description…

  2. Veli Bilgilendirme

    November 28, 2013 at 5:13 PM

    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.

  3. hello, i just change your code litlle
    from :
    echo “”;

    to be:
    echo ” {$meta} />”;

    anf works fine.

  4. This is really awesome and i love that.. This is very unique thing you put on that post.. Thanks for sharing…

  5. Thanks for this dode ^ ^, but it doesn’t seem to work for the index page

  6. Mandailing Natal

    February 7, 2013 at 4:17 AM

    great code, its working for me :)

  7. Great! Thanks for your time sharing this. :)

  8. dude. today you saved my life. thank you!

  9. Cor van Noorloos

    August 25, 2012 at 4:57 AM

    Definitely interesting! I am/was using the_excerpt_rss(); instead but found it quite hard to trim.

  10. This would be a great example of creating aplugin for such a feature. It would me much, much better then using such a feature with a theme. What happens when you change a theme or want to test some themes? You always need to remember what code you added and copy that in to the new theme. With a plugin you can change themes as much as you like without the need of always copying some code in to the new functions.php file.

    I would recommend to always use plugins for extra features for your site.

    • rilwis

      August 20, 2012 at 12:04 AM

      I agree. The technique here not only applied to themes, but maybe used in SEO plugin. Every big thing is built on small things like this ;)

  11. Is there anyway to include the title of the post on the description?


    • rilwis

      August 18, 2012 at 10:49 PM

      Yes, you always can. Just include this line:

      $meta .= $post->post_title;
      echo ...
      • Does it also get some excerpts on the content?

        For example this is my post

        Title: Example Post Titlte
        Content: This is only example post item in my wordpress blog..

        The meta decription should be : Example Post Titlte. This is only example post item in my wordpress blog..


    • rilwis

      August 19, 2012 at 1:02 PM

      No. The post_content is the content only. You can use post_excerpt to receive excerpt.

      • I already got it now.. but the problem. will this conflict on platinum seo pack?

        I already installed it but I cannot do what I want like on this post.


      • rilwis

        August 20, 2012 at 12:03 AM

        I haven’t tested with Platinum SEO Pack, but I think that popular plugin can handle the problem automatically. I’ve been using Yoast SEO plugin, and it’s pretty cool. You can try it, too.

  12. How to add ” wp_head() in your theme header.” ?

    • rilwis

      July 8, 2012 at 9:51 PM

      Simply open the `header.php` file of your theme, and add <?php wp_head(); ?> just right before </head>.

  13. thanks for the code but i have some problem with it like if you post image in post starting it show Line Space and also description is in ( ‘ ) i want ( ” )

  14. Hi,
    From where will descriptions be taken?


  15. Love the idea, but noticed your code was checking for a single post. I am assuming that this will work with pages as well, removing that single check… Correct?

    • Tran Ngoc Tuan Anh

      March 21, 2012 at 4:13 AM

      Removing that check make the function works in all pages, including search, archive, blog page. If you want it works in only post & pages, you can use check is_singular

    • Tran Ngoc Tuan Anh

      March 21, 2012 at 4:25 AM

      Anyway, I have updated the snippet to make it work with pages, too.

Leave a Reply

Your email address will not be published.