WordPress page keywords and descriptions

These days, the page meta tags for keywords and descriptions are generally ignored for SEO, as search engines get more and more sophisticated and as people try and cheat rankings by padding their meta tags with words. I haven’t been paying attention to it myself, but that’s not any reason to get shoddy with meta tags!

Sometimes page descriptions do matter: they show up in search result pages, when sharing links in Facebook, and other small things. For a WordPress blog though, these aren’t usually baked in by default. Your keywords and description are actually your website’s description!

That isn’t very helpful when you’re sharing a link and a description of your whole blog shows up instead of what the link is all about. :/

So I sat down one day and tried to figure it out for my blog. Because of how my blog is set up, I wanted the following things:

  • Category pages should reflect keywords and description of the whole category.
  • Single post pages should reflect an excerpt of the post for the description, and post category and post tags for the keywords.

And here is my function. :)

function get_keywords_description( &$keywords, &$description ) {

    if( is_home() ) {
        // home page should have blog-wide description and keywords
        $description = get_bloginfo('description');
        $keywords = "list,of,keywords,here";
        return;
    }
    
    if( is_category() ) {
        //categories you actually want to track/"specialize"
    	$CATEGORIES = array( 3, 4, 5 );

    	global $post;
    	$categories = get_the_category( $post->ID );
    	$current_top_category = 0;
    	foreach( $categories as $c ) {
    		if( in_array( $c->cat_ID, $CATEGORIES ) ) {
    			$current_top_category = $c->cat_ID;
    			break;
    		}
    	}
        $cat_info = get_category( $current_top_category );
        $description = "{$cat_info->name}: {$cat_info->description}";
        switch( $current_top_category ) {
            case '3' :
                $keywords = "list,of,keywords,here";
                break;
            case '4' :
                $keywords = "list,of,keywords,here";
                break;
            case '5' :
                $keywords = "list,of,keywords,here";
                break;
        }
        return;
    }
    
    // not home page, and not category page
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        $description = get_the_excerpt();
    endwhile; endif;
    rewind_posts();

    global $post;
    $tags = get_the_tags($post->ID);
    $cats = get_the_category($post->ID);
    foreach( $tags as $t ) {
        $keywords .= "{$t->name}, ";
    }
    foreach( $cats as $c ) {
        $keywords .= "{$c->name}, ";        
    }
    $keywords = rtrim($keywords,', ');
}

Actual usage is pretty straightforward.

<?php
$keywords = $description = '';
get_keywords_description( $keywords, $description );
?>
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="<?php echo $keywords; ?>">

It’s not as elegant as I’d like–look at those horrid loops, and the use of the global $post–so if you have any suggestions on how to make it better, let me know!