Auto generate meta description tag in WordPress without plugins

Meta Description SEO 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() )
        return;

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

25 Comments

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

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

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

    anf works fine.
    thanks

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

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

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

    Reply
    • 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 ;)

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

    Thanks!

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

        Thanks!

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

        Thanks!

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

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

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

    Reply
  8. 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?

    Reply

Leave a Reply