Use Wordpress custom fields for your meta tags

Today I realized my posts needed proper jargon in the description and keywords meta tags for uber SEO magic. What is the ‘right’ way to do this in WordPress? I found a bunch of plugins and other complicated methods, but I thought there should be an easy way with custom fields. I ended up on a forum post, which was exactly what I was looking for. You just create a couple custom fields: description and keywords, then print out the values of the fields in the header. So I dumped this into my theme’s header.php:

<meta name="description" content="<?php if(get_post_meta($post->ID, "description", true) !='' ) echo get_post_meta($post->ID, "description", true); else bloginfo('description');?>" />
<meta name="keywords" content="<?php if(get_post_meta($post->ID, "keywords", true) != '') echo get_post_meta($post->ID, "keywords", true); else echo strtolower(get_bloginfo('name'));?>" />

It works on the single post page, but it does not properly display the default on aggregation pages like the front page. This is fixed by adding the is_single() function to the if statements:

<meta name="description" content="<?php if(is_single() && get_post_meta($post->ID, "description", true) !='' ) echo get_post_meta($post->ID, "description", true); else echo 'My description';?>" />
<meta name="keywords" content="<?php if(is_single() && get_post_meta($post->ID, "keywords", true) != '') echo get_post_meta($post->ID, "keywords", true); else echo 'my, keywords, here';?>" />

Yay.

Discuss

1
darby simon  July 7, 2009 at 12:29 pm

um.. genius. i’ve been hunting for this kind of info for two months! thank you thank you. seriously worked like a charm. I just plugged it into my header and bam. all of the other plugins were janky, so I like REAL code!! Thanks so much!!

2
Martin  July 24, 2009 at 7:50 pm

Thank you for getting me to think. I took your code and augmented the following to allow for “page” posts, spaces and comma.


&lt;meta name="description" content="ID, "description", true)) echo(get_post_meta($post-&gt;ID, "description", true)); ?&gt;ID, "description", true)) { echo ' '; } ?&gt;My base description." /&gt;

&lt;meta name="keywords" content="my, base, keywordsID, "keywords", true)) { echo ', '; } ?&gt;ID, "keywords", true)) echo(get_post_meta($post-&gt;ID, "keywords", true)); ?&gt;" /&gt;

3
Martin  July 24, 2009 at 7:54 pm

Let’s try this again,… Thank you for getting me to think. I took your code and augmented the following to allow for “page” posts, spaces and comma.

<meta name=”description” content=”ID, “description”, true)) echo(get_post_meta($post->ID, “description”, true)); ?>ID, “description”, true)) { echo ‘ ‘; } ?>My base description.” />

<meta name=”keywords” content=”my, base, keywordsID, “keywords”, true)) { echo ‘, ‘; } ?>ID, “keywords”, true)) echo(get_post_meta($post->ID, “keywords”, true)); ?>” />

4
pedro  January 20, 2010 at 7:49 pm

hi. i have no php knowledge. can you help me changing my header.php to have meta description and keywords, on pages, through custom fields, like you are telling here?

thank you

 | 
 | Search Results
 | Author Archives
 | 
 | 
 | Archive | 
 | Archive | 
 | Tag Archive | 

<meta http-equiv=”Content-Type” content=”; charset=” />
<link rel=”stylesheet” type=”text/css” href=”" media=”screen” />
<link rel=”alternate” type=”application/rss+xml” title=”RSS 2.0″ href=”<?php if ( get_option(’woo_feedburner_url’) “” ) { echo get_option(’woo_feedburner_url’); } else { echo get_bloginfo_rss(’rss2_url’); } ?>” />
<link rel=”pingback” href=”" />


<script type=”text/javascript” src=”/includes/js/pngfix.js”>
<script type=”text/javascript” src=”/includes/js/menu.js”>
<link rel=”stylesheet” type=”text/css” href=”/ie6.css” media=”screen” />

comment

Your email is never shared because I like you.

*
*