Auto generate meta description tag in WordPress without plugins

Auto generate meta description tag in WordPress without plugins

Meta description tag is an important factor for Search Engine Optimization (SEO). Search engines use it for display page snippet in Search Engine Result Pages (SERPs). Via meta description tag (snippet) 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 built-in feature for auto generation meta description tag. But you can do it yourself without any plugins, and here it is!

Simply paste the following code into your functions.php file of your theme:

add_action( 'wp_head', 'gen_meta_desc' );
function gen_meta_desc()
    global $post;

    if ( ! is_singular() )

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

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

Change the 125 to number of characters you want to show in meta description tag.

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


  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