Most search engines don’t consider meta description in calculating the importance of a web page, but it’s still used to create snippets of text that users see when your web page appears in search results. The meta description of a page is found between the <head>
and </head>
tags. It’s coded as:
<meta name="description" content="Insert a short description of your website here." />
Most of the free WordPress themes available out there don’t have the Meta Description Tag and users have to opt for a plugin to fix this issue. In this post, I will show you a quick and simple way to optimize your WordPress site’s Meta Description using the existing ‘Excerpt’ feature. With it, WordPress will autmatically display correct (the one you set) Meta Description for each post, category, page and homepage.
Below is the complete code that you need to add to your WordPress theme. Open header.php
file and insert the PHP code between <head>
and </head>
tags, preferbly below <title>
and </title>
tags. If already there’s a meta description tag, just delete it and paste this code.
<?php if (is_single()) { ?> <meta name="description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" /> <?php } else if( is_category() ) { ?> <meta name="description" content="<?php echo strip_tags(category_description(get_category_by_slug(get_the_category())->term_id)); ?>" /> <?php } else if(is_home() || is_page()) { ?> <meta name="description" content="Insert a short description of your website here."> <?php } ?>
So how does this code work?
WordPress simply extracts the text that you wrote in the excerpt text area of a post and assigns it as the meta description. For example, if you have a post entitled “My Birthday” and in the excerpt area you typed in “Had a rocking birthday this year!”, it’s meta description will show up like this:
<meta name="description" content="Had a rocking birthday this year!" />
Code explanation:
Post description as meta description
We are using the get_the_excerpt();
function to extract text from the Excerpt field in the post editor.
By default, <p>...</p>
tags are also inserted along with the excerpt. To get rid of those we use another function strip_tags();
.
<?php if (is_single()) { ?> <meta name="description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>" />
If the excerpt field of a post is left blank, the meta description would also be blank. It would then show up like this in the source code:
<meta name="description" content="">
Category description as meta description
For the dynamic category page, we will use the description that you write in the Edit Category page. Rest the code works similar to the previous one.
<?php } else if( is_category() ) { ?> <meta name="description" content="<?php echo strip_tags(category_description(get_category_by_slug(get_the_category())->term_id)); ?>" />
Default meta description
Here, write the default meta description that you want for all other pages on your site.
<?php } else if(is_home() || is_page()) { ?> <meta name="description" content="Insert a short description of your website here.">
Optional:
Page description as meta desctiption
As WordPress doesn’t all us to write excerpts for pages, you can write custom descriptions for any specific page the following following way:
<?php } else if(is_page(3)) { ?> <meta name="description" content="Insert a short description of your website here.">
Here ‘3’ is the page ID.
That’s all! If you face any problems with the codes, let me know in the comments and I will try to help you out.