Adding Dynamic Keywords on Meta Keyword Tag

I think it's nearly impossible to find a person now a days, who is involved with blogging or web design related works yet never heard of the term SEO (Search Engine Optimization). Trust me, the guy who just got started with the creative web design world yesterday, he also knows what is SEO. It is not a rocket science anymore.

If you are familiar with the typical HTML meta tags then you should know by now that how most search engines (Google, Bing, Yahoo! etc.) crawls meta data from your site which helps them to rank your site. Especially if you have a blog site, where you are changing, adding contents nearly everyday, usage of these meta tags could be a very big issue.

If you take look at the source code of your WordPress site or your blog posts, chances are there that your theme is also using meta "Keywords" tag as well which might look something like the line mentioned below. Don't be afraid, you can easily find this line on your theme's "header.php" (generally) file.

<meta name="keywords" content="words, words, words">

If you don't know where to find header.php file, then simply log in to your WordPress site and then go to "Appearance > Editor". Look at the right hand side of the screen, you can't miss it.

Now, Using a very simple snippet code you can make it dynamic which will not only help you to optimize your posts but also your site's pages. Before I reveal the code let me explain what it does.

First of all, it will change the meta keywords on all the pages (each permalinks) of your site. This code will generate a certain number (don't worry, you can change the number. Look at the code carefully. We are using 40 keywords here.) of top (mostly used) keywords used on your blog posts. In terms of single post, it will only display the actual keywords used on your post. Here is the code.

if (is_single()) { 
   $posttags = get_the_tags();
   if ($posttags) {
      foreach($posttags as $tag) {
         echo $tag->name.', ';
} else { 
   $tags = get_tags(array('orderby' => 'count', 'order' => 'DESC')); 
   $xt = 1; 
   foreach ($tags as $tag) {
      if ($xt <= 40) { echo $tag->name.', '; }

However, there are many ways to customize this code. You can even manually add certain keywords for individual pages if you want. But we will take about it later, on some other post. Till then stay tuned for more WordPress related snippets.


Commenting is disabled.