Adding a Dynamic Meta Description to Your WordPress Theme

Any good developer knows the importance of good SEO structure. Having certain META tags in your header.php file can make all the difference in how Google and other search engines prioritize your pages.

Meta keywords serve absolutely no purpose. Meta descriptions, however, are one of the most important tags to have.

Because WordPress uses one single header file for it’s entire theme, you are forced to create a dynamic script that changes for every page. We can accomplish this with PHP.

You can either edit the file through a FTP editor, or through WordPress, your choice. We’ll assume that you are editing it through WordPress from here on out.

Open the Header.php File

Log into WordPress. Go to Appearance, then Editor, and open the header.php file.

Where to Place the Meta Description In Your Header

The header file of your site consists of a structure like this :

<html>
 <head>
  <title>Your Site's Title</title>
 </head>
 <body>

In between the <head></head> is where your meta description tag will be placed. It doesn’t matter if it’s placed before the title, after the title, before the css files, after, it makes no difference.

Creating Your Meta Description for SEO

Typically a meta description tag looks like this :

<meta name="description" content="A nice description for your page here." />

The content are should be filled with a few sentences that describe the particular page using targeted keywords for your audience.

Since we can’t just do this for every page, we need to make this description dynamic.

There are a few methods for incorporating a meta description dynamically, but my favorite is always taking a small excerpt from your actual post. The first few sentences of your post are typically geared towards what the page is about, so taking those and placing it in the meta description is a pretty good way of ensuring that they both make sense and are related to what the search engine is looking for.

There is a template tag already included in WordPress that does this. That tag is :

<?php the_excerpt_rss(); ?>

This pre-determined piece of code grabs the “excerpt” or beginning of your post and puts it in place of the tag. I include the “_rss” part because an RSS feed is the absolute barebones of content. No images, no tags, no links, just the core words and nothing else. That’s what you want.

So, your meta description in the header should look something like this now :

<meta name="description" content="<?php the_excerpt_rss(); ?>" />

That’s all it takes to make a dynamic meta description for every post!

Keeping a Meta Description for the Home Page or Index Page

This works good n’ well until you realize that your home page now gets indexed by the most recent post you have on your site. Whatever is the most recent becomes the meta description to search engines and that may not be what you want. You may want a consistent slogan or description for your regular site, and then a description calling to the excerpt of every post and page.

We can accomplish this with PHP as well!

Create the Page/Post Statement

We’ll start by making an “If statement” using WordPress’s own pre-determined loop. Basically, we’ll write something like this :

<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

What this says, essentially, is “if this is a post, or a single page, do the following….”

Create the Home Page Statement

Then, we’ll need and “else-if statement” for the cases when it’s NOT a post or page.

<?php endwhile; endif; elseif(is_home()) : ?>

This is basically saying, “End the last if, but now, if the page is the home page, do the following…”

What would you want the home page to say, though? Well, you have a few options. You can either fill in your own description, or you can actually have the “Slogan” called through PHP and placed in the description. The benefit of that is being able to edit your slogan in Settings of WordPress, as opposed to constantly going to your file editor and changing it. We’ll go with that. It looks like this :

<meta name="description" content="<?php bloginfo('description'); ?>" />

Ending the If Statement, and Final Output

We’ll place a final “endif” statement to close down the logical test, and our dynamic header will be complete! Your final code should look like this :

<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<meta name="description" content="<?php the_excerpt_rss(); ?>" />

<?php endwhile; endif; elseif(is_home()) : ?>

<meta name="description" content="<?php bloginfo('description'); ?>" />

<?php endif; ?>

In Conclusion

So, in summary, what do we have? Well, we have a statement that basically says, “If this is a page or a post, grab the first few lines of code and make that our meta description. If it’s the home page, use our Slogan from WordPress Settings.”

That’s all it takes, and now you have an excellent tool for SEO. Good luck!

3 Comments

  • @JonathanCalos – It looks like you got it working! Nowadays though, I recommend just using Yoast SEO for a WordPress site as it simplifies a lot of the code editing and settings.

  • Hello,
    I have tried adding a dynamic meta description to my website: https://jocalendars.com
    but nothing has worked.

    Any assistance will be appreciated.
    Thanks

  • You can try this code

    <meta name="description" content="” />

    <meta name="description" content="” />

Leave A Comment

Your email address will not be published. Required fields are marked *