How To Add Dynamic Meta Description to WordPress

This post is all about adding a Dynamic Meta Description to your WordPress Theme. You can try and add meta description code in the header.php file of your WordPress theme. But it would result into a static meta description. Which means the meta description will remain the same for each and every page on your WordPress site.

But if you want to tweak and optimize your site for search engines then you must add dynamic meta description to your WordPress Theme.

This can help you to have different meta descriptions for your home page, static pages, and blog posts. Also this can help you improve the SEO of your site.

First of all, I don’t know if you have any idea of what is a Meta Description. So i will write few more lines about it before we actually get to the Dynamic Meta Description part.

What is Meta Description?

Meta Description of a page is like a very brief and concise information of what the page is all about. It is not a major factor in determining the search engine rank. But it can surely help you to get good search engine rank for your website.

wordpress dynamic meta description for home pageIn the image above, you can see the green text is what Meta Description looks like in Google Search Results. Now lets see how to add a Meta Description to WordPress Theme.

Adding Static Meta Description To WordPress Theme

We’re going to add a simple meta description to header.php of WordPress Theme. To do so, follow the steps given below –

  • 1) Open Administrators Dashboard > Appearance > Editor > header.php
  • 2) Add the following Code just before the closing head tag ( </head> )

[php] <meta name=”description” content=”Blog Description Goes Here” /> [/php]

  • 3) Replace Blog Description Goes Here with your Blog Description Text.
  • 4) Update file. And you’re done.

But since header.php is loaded on every page of your WordPress site, the meta description will remain the same on every page of your site. Which means it is a Static Meta Description.

Adding Dynamic Meta Description To WordPress Theme

Lets see  how to add a dynamic meta description to the WordPress Theme. The basic concept is that we are going to use if else code to detect which page is being loaded. And with that information we can then determine an appropriate or dynamic meta description for that respective page.

  • 1) Open Administrators Dashboard > Appearance > Editor > header.php
  • 2) Add the following Code just before the closing head tag ( </head> )

[php] <?php if ( (is_home()) || (is_front_page()) ) { ?>
<meta name=”description” content=”Blog description goes here” />
<?php } elseif (is_single()) { ?>
<meta name=”description” content=”<?php the_excerpt();?>”/>
<?php } ?> [/php]

  • 3) Replace Blog Description Goes Here with your Blog Description Text.
  • 4) Update file. And you’re done.

This code helps you to show a Static Meta Description for Home Page as well as Front Page of your WordPress site. But it will generate a dynamic meta description for all your WordPress posts.

The first part of the code checks that if page is home page or front page then display Blog Description. While the second part of the code checks whether the page is a single post page, and if so then use an excerpt of the post as Meta Description.

Now the Meta Description is different for home page as well as for every blog post. And thus we have successfully implemented a Dynamic Meta Description in a WordPress theme.

Leave a Reply