WordPress SEO – optimize your META Keywords & Description manually

I am a big fan of the “All In One SEO Pack” plugin for WordPress. It really helps you make sure your blog is as robot-friendly as it gets. I’m not going to give any details because this post is not about the AIOSP plugin. I’m loving the idea of having such great plugins helping you with what could easily get a really messy task. On the other hand I like to help out myself and write small pieces of code that give me exactly the results I want without having all sort of third-party plugins doing it for me.

What I will try to do is to have 100% dynamical META keywords and descriptions for every page of the blog without using any plugins.

Let’s take a look inside the header.php file of your theme for instance. You should see the lines below somewhere in your file however bare in mind that depending on your theme the following lines might be slightly different:

<meta name="keywords" content="bla,bla,bla" />
<meta name="description" content="bla,bla,bla" />

Before going any further I should make something clear: the keywords attribute should be relevant to the content of the page. That’s a must and everyone knows why. So before defining some static keywords blog-wide (I suggest that 8 should be the maximum number of keywords used) in the header.php file that basically define the area covered by your blog (sports, traveling, etc.) you might want to consider the fact that these keywords will be used for all your posts and pages.

A common form for the description attribute is the following:

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

description which you can easily define in the admin interface. Well, that again doesn’t serve you any good as the description of your blog might not accurately summarize a certain post (probably none of them).
OK so now we have some static keywords and a general description. Both of them are carried around on your entire blog without actually having something to do with a random particular page. Keywords and description that don’t match the content of the page ? I don’t think anyone wants that. That’s about it with the static keywords and description; problem which I’ve seen on many blogs.

As we move further I will concentrate more on the keywords attribute and leave the description at the end. I for example have three methods for defining the tags for an article:

  • Using the All In One SEO Pack form that shows up whenever you create/edit an article
  • Using the built-in Custom Fields form that like AIOSP form shows up right under the WYSIWYG editor
  • Using the good old Tags form that I’m sure everyone is familiar with.

Of course depending on which plugins you have installed you might have other possibilities of defining tags for a post.

Instead of using some complicated methods to achieve this simple task why not use the already built-in form. You can then call the the_tags() function and have the post’s tags shown right under its content. Of course that’s nothing new, everyone is doing it since like forever. OK so why not using those tags as keywords ? Yesterday it sounded like a good idea for me so I said to myself why not try to put those tags as keywords in the header.php file of my theme. What will we accomplish with this ? Well… nothing much actually except for the fact that all the META keywords will perfectly match the content of any page of my blog.

Enough with this already, let me give you the code that does all the magic.

1
<meta name="keywords" content="
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
//here we make sure that what's to come will affect only the pages that show the article content
 if (is_single()){ 
 
//here we call the function that is responsible with passing numerous post attributes (tags, content, etc.)
the_post(); 
 
//here we get the array returned by the function
$posttags = get_the_tags(); 
//we make sure that our post has tags
if ($posttags) {  
	//we will use this variable to count how many tags we have
        $i=0; 
	//with every loop we will assign each element to the $tag variable 
        foreach($posttags as $tag) { 
		 //now we will store each keyword into a more friendly array placing a comma after each one
                 $str[$i]=$tag->name . ','; 
		 //here we make sure the first keyword won't be overwritten with the next keyword
                $i++;
	}
}
//here we will store the length of the last word
$len=strlen($str[$i-1]);
 
//here we will make sure that the comma after the last keyword is removed so that we have a tidy keyword list
$str[$i-1]=substr($str[$i-1],0,($len-1)); 
 
 //here we will simply output the list of keywords in its final state
for ($k=0; $k < $i; $k++){ 
	echo $str[$k];
}
}
//if this is not an article page then use those predefined home keywords
else 
{
echo "here will go the keywords matching your home page";} 
?>
39
" />

Due to a tricky set of instructions in GeSHi I had to split the code into those three blocks above. The line numbering however is correct so you should assemble the code easily.

Now that you copied the code above into your header.php file you will see that your articles aren’t showing up on their pages. Why is that ? Because you didn’t call the_post() function in a proper loop. Actually there’s no loop at all there. The actual loop used for displaying the post’s content is in your single.php file. Now we have to undo all the “damage” we caused. Don’t worry, nothing bad happened. All you need to do is to place the following code before your loop (if you don’t know what the loop is, here’s an explanation):

<?php rewind_posts(); ?>

There you go ! Now everything should be working again.

This might not be a viable solution for some of you out there but for me it definitely does the trick. I’m sure that some of you who are advanced PHP programmers will find or already know a simpler method for what I’ve accomplished here. Hopefully the commented lines will help you understand the code and not get in your way while trying to understand it.

As I promised I will now offer a variant of generating the description attribute entirely dynamic, again strictly based on the post page. I thought that a good description would be an actual excerpt of the article. It’s more like a preview after all. The default excerpt (if you don’t write a specific one in the Excerpt box under the WYSIWYG editor) contains the first 55 words of your article. No HTML tags, no nothing, just plain text (well, not actually). So what I’ve accomplished is to have a specific, unique description, again for each and every page of my blog. Here is the code:

1
<meta name="description" content="
2
3
4
5
6
7
8
9
10
11
12
13
<?php
//we make sure that what's to come will affect only pages that show the article content
if (is_single()){
//here we will store the real "tagless" output of the excerpt
$excerpt = strip_tags(get_the_excerpt());
        //and of course here we will output the final excerpt 
        echo $excerpt;
}
//in case we are on the home page for instance we will display the default description
else{
bloginfo('description');
}?>
14
" />

Again, be advised of the code splitting I had to do. Because we called the_post() function in the keywords attribute (which in this case has to be before the description attribute) we don’t need to apply “the fix” again in the single.php file. However if you want to try only the code that affects the description then you will need to call the_post() function right after is_single() function, exactly as shown in the code related to keywords attribute.

Some would say that this whole thing isn’t such a big deal but actually it’s just one idea I had yesterday which I am blogging today. The possibilities are endless. We are limited only by our knowledge and imagination. Please feel free to comment on this idea of mine. You might have something to say that really matters. And as a wrap up, I really hope you enjoyed reading this article and hopefully it will help you in a way or another.

Social bookmarking:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Mixx
  • Sphinn
  • Reddit
  • Live
  • Twitter
  • Yahoo! Bookmarks

This entry was posted in scripts, wordpress and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Hy there. Tried your great tricks on my wp website and unfortunately I got the following error mesasge somenthing like "header cannot be modified because of the files pluggable.php at line 890..."
    Any ideas what can go wrong?

    Thanks,
    Puiu
  • Andrei
    Great stuff, thank you. That "rewind posts" was killing me! I love having the description tag simple and clean without fat plugins.
  • wirul
    thank
    I'll use it in my wp site
  • Thanks for sharing. Very cool!
  • Dan
    Thank you both for your remarks.
  • Good post!I also have something about SEO to share. Check out my website.124
  • Yes other than having niche and original content the other important thing is to make all of your website pages SEO friendly and this helps a lot.
blog comments powered by Disqus