Home » Wordpress » Wordpress SEO » How to automatically create meta description from content in wordpress

By default, WordPress do not add a <meta description> tag to your blog. While not necessary, some SEO experts insists that this tag is important for your site SEO. So what about generating one using your post content? Here is a cool piece of code to do it easily.

The Meta description tags should have relevant keywords to the article to attract visitors to your site.

In WordPress it can be done by using SEO plugins, but if you don’t have these plugins installed or forgot to add the meta description to this plugin then here is the WordPress snippet for you.

This snippet will automatically take the first 125 characters of your content and add this into the meta description tag for the page.

Paste the following code into your functions.php file:

function create_meta_desc() {
    global $post;
if (!is_single()) { return; }
    $meta = strip_tags($post->post_content);
    $meta = strip_shortcodes($post->post_content);
    $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
    $meta = substr($meta, 0, 125);
    echo "<meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');

Thanks to Paul for this handy snippet!

Array ( [0] => Wordpress SEO )

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.