meta description

Auto generate meta description without plugins in WordPress

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!

Updated: the code below is outdated. Please use the Slim SEO plugin to get the job done automatically.

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() ) return; $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. 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?

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

  2. 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 ( ” )

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

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


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


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

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

    1. 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 😉

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

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

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

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

    anf works fine.

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

  8. Oh my God. You saved me from a big trouble!

    I was using Greg’s High Performance SEO, but there was a difficulty in generating auto Description and meta tags. But, thanks to you i got success in getting that so easily:) God bless you !!

    Prior to this i was using SEO Yoast, and everything was perfect, but this plugin is too bad for heavy load sites where people submit their own content (i have a classifieds site – ) . So, had to deactivate that to lower the hosting burden..i have around 10K it had became really painful .

Comments are closed.