Good point about the rewind; I wouldn’t have thought of that and been scratching my head when things were messed up.
I had some time to play with it and I got things working.
Keep in mind, one of the benefits of using plugins is you don’t have to hack the files again if you upgrade or change your theme.
And the “inside the Loop requirement” is very true. The docs say custom fields work with “posts or pages” but I guess they assume they will be used in the page content, not the header.
Anyway I added custom fields to a post and a page using:
meta-description
meta-keywords
and added some test text.
Then in the header template I put the code that writes the meta tags, inserting the custom field values for the content values.
*This is very crude hackish code, and it will “break” if you use the custom fields more than once per page.
<title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title>
<?php
if (have_posts()) : the_post();
$custom_fields = get_post_custom();
$custom_meta_description = $custom_fields['meta-description'];
$custom_meta_keywords = $custom_fields['meta-keywords'];
if ( !empty($custom_meta_description[0]) )
{
echo "<meta http-equiv='description' content='" . $custom_meta_description[0] . "' />\\r\
";
}
if ( !empty($custom_meta_keywords[0]) )
{
echo "<meta http-equiv='keywords' content='" . $custom_meta_keywords[0] . "' />\\r\
";
}
rewind_posts(); // Thanks Azuaron !!
endif;
?>
and I get this view-source for a post
<title>Hello world! « test blog</title>
<meta http-equiv='description' content='initial default blog post' />
<meta http-equiv='keywords' content='hello world post' />
and this for a page
<title>About « test blog</title>
<meta http-equiv='description' content='this page talks about me' />
<meta http-equiv='keywords' content='all, about, testing' />
You need to remember to put in the commas where you want them.