<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Edward Stafford&#187; seo</title>
	<atom:link href="http://www.edwardstafford.com/tag/seo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edwardstafford.com</link>
	<description>Technologist, Artist, and Geek</description>
	<lastBuildDate>Thu, 12 Aug 2010 17:59:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WordPress: Use Custom Fields To Add Keyword Metadata to Your Posts</title>
		<link>http://www.edwardstafford.com/2010/02/04/wordpress-use-custom-fields-to-add-keyword-meta-data-to-your-posts/</link>
		<comments>http://www.edwardstafford.com/2010/02/04/wordpress-use-custom-fields-to-add-keyword-meta-data-to-your-posts/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 11:00:10 +0000</pubDate>
		<dc:creator>Ed Stafford</dc:creator>
				<category><![CDATA[Code Mode]]></category>
		<category><![CDATA[blogs]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[How-to]]></category>
		<category><![CDATA[keywords]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[thematic]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.edwardstafford.com/?p=258</guid>
		<description><![CDATA[One of the short-comings with using WordPress is that it does not provide an easy, built-in way to include metadata for your web page descriptions and keywords (and rightfully so). Why Not? The reason is simply that WordPress cannot read your mind. I know it&#8217;s hard to believe when you consider what you can do [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_276" class="wp-caption alignleft" style="width: 262px"><img class="size-medium wp-image-276   " title="Keywords for edwardstafford.com" src="http://www.edwardstafford.com/wp-content/uploads/keywords-black-300x120.png" alt="Keywords at edwardstafford.com" width="252" height="101" /><p class="wp-caption-text">Keywords for edwardstafford.com</p></div>
<p>One of the short-comings with using WordPress is that it does not provide an easy, built-in way to include metadata for your web page descriptions and keywords (and rightfully so). Why Not? The reason is simply that WordPress cannot read your mind. I know it&#8217;s hard to believe when you consider what you can do with wordpress, but it&#8217;s true. The issue with Description and Keyword page metadata is that, to be truely effective, it should be created to  describe the content found on each individual page. It&#8217;s how search engines like google determine how to categorize and index each page. Now, there are some SEO &#8220;experts&#8221; who will argue that this information is not very relevant anymore, and I do agree with that for the most part, but there are still SEO benefits to including this metadata vs. not including it at all.</p>
<p>I&#8217;ve been giving this some thought lately and developed a couple ideas of how to add these features into a wordpress site without too much difficulty. A bulb went off in a moment clarity when I started to think about using the Custom Fields to store page specific metadata. I was even naive enough to think I was on to something new (should have known better) but as I started researching some ideas, I realized there were others already doing similar things. Oh well, a minor detail. I took my own approach to the idea anyway, if for no other reason than a learning exercise. Ultimately, this could be added as a premium feature to any custom theme using a couple hooks and some custom theme options magic.</p>
<p><span id="more-258"></span></p>
<h2>Okay smarty-pants! So how do we include description and keyword metadata?</h2>
<p>The solution isn&#8217;t difficult to implement and can be used to add page specific metadata inside the &lt;head&gt; tag of any wordpress page. In this example, I&#8217;m going to show you a simple solution, but with some creativity and a little thought, you can expand this to provide a more robust solution for your own needs.</p>
<h2>Part I: Defining a Custom &#8220;Keywords&#8221; Field</h2>
<p>Custom Fields are a little used gem found in the Posts and Pages page of the WordPress dashboard / admin back end and available when you add a new post or page, or edit an existing post or page.</p>
<div id="attachment_263" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-263 " title="New Custom Field" src="http://www.edwardstafford.com/wp-content/uploads/custom-fields1-300x152.png" alt="Create a new custom field" width="300" height="152" /><p class="wp-caption-text">Create a new custom field</p></div>
<p>To create a new custom field to use for your page or post keywords, scroll down to the <strong>Custom Fields</strong> section below the main editor and click the &#8220;<strong>Enter new</strong>&#8221; link.</p>
<p>That action will activate the input fields for &#8220;Name&#8221; and &#8220;Value&#8221;.</p>
<p>In the Name field type the name identifier you&#8217;d like to use. This identifier/name will be used to reference the custom field name/value pair later so make it something relevant to avoid confusion. For this example, I am using &#8220;<strong>keywords</strong>&#8220;.</p>
<p>In the Value field, type the string of keywords or phrases that you want to use as the keyword metadata string for the page/post. For this example, I am using <strong>wordpress, seo, metadata, custom fields</strong></p>
<div id="attachment_265" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-265" title="Add New Custom Field Inputs" src="http://www.edwardstafford.com/wp-content/uploads/custom-fields3-300x89.png" alt="" width="300" height="89" /><p class="wp-caption-text">Add New Custom Field Inputs</p></div>
<p>When you&#8217;re done, hit the &#8220;<strong>Add Custom Field</strong>&#8221; button.</p>
<p>If you have already created a <strong>&#8220;keywords&#8221; Custom Field</strong> for a previous page or post, you will have the option of selecting it in future pages and posts from the Name drop down menu.</p>
<div id="attachment_266" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-266" title="Custom Fields Menu" src="http://www.edwardstafford.com/wp-content/uploads/custom-fields-menu-300x114.png" alt="Custom Fields Menu" width="300" height="114" /><p class="wp-caption-text">Select from Custom Fields Menu</p></div>
<p>Select the Keywords item from the menu and then add the keywords or phrases for your new page to the Value field. When your done, make sure you hit the &#8220;<strong>Add Custom Field</strong>&#8221; button to confirm it.</p>
<p>That completes Part I. You now have a functional Custom Field assigned to your post/page.</p>
<p>Next up! Adding and understanding the code that generates the keyword metadata tag.</p>
<h2>Part II: The Magic Code.</h2>
<blockquote><p>function set_keywords(){<br />
global $post;<br />
$keywords = get_post_meta($post-&gt;ID, &#8216;keywords&#8217;, true);<br />
$default_keywords = &#8220;Your, selection, of, default, keywords&#8221;;<br />
$metatag= &#8220;&#8221;;</p>
<p>if (empty($keywords)){<br />
$keywords = $default_keywords;<br />
}<br />
if (is_home() || is_front_page()){<br />
$keywords = $default_keywords;<br />
}</p>
<p>$metatag=&#8221;\t&#8221;;<br />
$metatag.= &#8220;&lt;meta name=\&#8221;keywords\&#8221; content=\&#8221;";<br />
$metatag.= $keywords;<br />
$metatag.= &#8220;\&#8221; /&gt;&#8221;;<br />
$metatag.= &#8220;\n\n&#8221;;</p>
<p>echo $metatag;<br />
}<br />
add_action(&#8216;wp_head&#8217;, &#8216;set_keywords&#8217;);</p></blockquote>
<p>Adding this block of code to your Theme&#8217;s <strong>functions.php</strong> file will create and insert page specific Keyword Metadata into inside the &lt;head&gt; tag of your pages and posts assuming you have completed Part I.</p>
<h3>How the code works</h3>
<blockquote><p>function set_keywords(){</p></blockquote>
<p>The first line declares our function and sets the name &#8220;set_keywords&#8221;.</p>
<blockquote><p>global $post;<br />
$keywords = get_post_meta($post-&gt;ID, &#8216;keywords&#8217;, true);<br />
$default_keywords = &#8220;Your, selection, of, default, keywords&#8221;;<br />
$metatag= &#8220;&#8221;;</p></blockquote>
<p>These four lines set up the variables that will be used later in the function.</p>
<p><strong>global $post</strong> allows the function to reference values stored in an array containing post information that was previously set by WordPress outside of the current function.</p>
<p><strong>$keywords = get_post_meta($post-&gt;ID, &#8216;keywords&#8217;, true)</strong> is up next and sets our variable, <strong>$keywords</strong> to a string value equal to the &#8220;<strong>keywords</strong>&#8221; <strong>Custom Variable</strong> if it was set for the current page in Part I by calling the wordpress function <strong>get_post_meta()</strong>.</p>
<p>get_post_meta() is passed three arguments.</p>
<ol>
<li>The post ID (<strong>$post-&gt;ID</strong>)</li>
<li>a Custom Field Name reference (<strong>keywords</strong>)</li>
<li>a boolean value (<strong>true</strong>)</li>
</ol>
<p>These three arguments tell get_post_meta() to grab the &#8220;<strong>keywords</strong>&#8221; Custom Field value for a specific page (<strong>$post_ID</strong> = a reference to the current page) and return a string result (<strong>true</strong>).</p>
<p><strong><span style="color: #999999;">Note</span></strong><span style="color: #999999;">: If the Boolean argument were set to &#8220;false&#8221;, get_post_meta() would return an array object instead of a single string.</span></p>
<p><strong>$default_keywords = &#8220;Your, selection, of, default, keywords&#8221;</strong> sets a set of default keywords to use when the $keywords variable is empty or not set, or when you just want to include a general set of keywords.</p>
<p><strong>$metatag = &#8220;&#8221;</strong> rounds up the variables for the function and is used to build the metadata tag string that will be inserted into the page &lt;head&gt; tag.</p>
<blockquote><p>if (empty($keywords)){<br />
$keywords = $default_keywords;<br />
}</p></blockquote>
<p>The next step in the code is a conditional statement. Here I use the common PHP empty() function passing the <strong>$keywords</strong> variable as an argument to check whether the <strong>$keywords</strong> variable contains a value. If the empty() function returns true it means that <strong>$keywords</strong> does not contain a usable value, or that is has not been set. If this condition is true, it will set the value of $keywords to that of $default_keywords.</p>
<p>If the Keywords Custom Field was set for the current page, then the get_post_meta() function should have set the $keywords variable to a usable value equal to the Keyword Custom Field value. The condition will return false and the variable retains it&#8217;s Custom Field value.</p>
<blockquote><p>if (is_home() || is_front_page()){<br />
$keywords = $default_keywords;<br />
}</p></blockquote>
<p>Next I have another conditional statement that uses WordPress functions  to see if the current page is either the home page &#8211; <strong>is_home()</strong>,  or the &#8220;blog&#8221; page &#8211; <strong>is_front_page()</strong> If the current page matches either of these pages, I want to assign the value of <strong>$default_keywords</strong> to the <strong>$keywords</strong> variable.</p>
<p>The reason I do this is because the content on these pages is more dynamic and has a tendency to change often, effecting keyword relevancy. So I want to provide a generic set of default keywords instead of page content specific keywords.</p>
<p><strong><span style="color: #999999;">Note</span></strong><span style="color: #999999;">: It is possible, using more in-depth techniques not covered here, to also include the Keywords Custom Field for these pages and enable a set of more targeted keywords for them.</span></p>
<blockquote><p>$metatag=&#8221;\t&#8221;;<br />
$metatag.= &#8220;&lt;meta name=\&#8221;keywords\&#8221; content=\&#8221;";<br />
$metatag.= $keywords;<br />
$metatag.= &#8220;\&#8221; /&gt;&#8221;;<br />
$metatag.= &#8220;\n\n&#8221;;</p></blockquote>
<p>These lines are used to build the actual text string creating the metadata tag for our page&#8217;s &lt;head&gt; tag.  for example, if our page contained a keyword Custom Field with a value of  &#8221;keyword1, keyword2, keyword3&#8243;, the output for a blog post or single page would be:</p>
<p><strong>&lt;meta name=&#8221;keywords&#8221; content=&#8221;keyword1, keyword2, keyword3&#8243; /&gt;</strong></p>
<blockquote><p>echo $metatag;<br />
}</p></blockquote>
<p>Wrapping up the function we have <strong>echo $metatag </strong>followed by the closing bracket bringing our function to a close.  <strong>echo $metatag</strong> simply writes the output of the <strong>$metatag</strong> variable to the source of your web page inserting the keyword meta tag in the page&#8217;s &lt;head&gt; tag.</p>
<blockquote><p>add_action(&#8216;wp_head&#8217;, &#8216;set_keywords&#8217;);</p></blockquote>
<p>And finally bringing this code bit to an end is <strong>add_action(&#8216;wp_head&#8217;, &#8216;set_keywords&#8217;)</strong>. The add_action() function is a wordpress function used to hook your custom function to a WordPress action. In this case we are hooking our function <strong>set_keywords</strong> to the wordpress action <strong>wp_head</strong>. This is the piece that actually inserts your meta tag into the page &lt;head&gt; tag.</p>
<p><strong><span style="color: #999999;">Note</span></strong><span style="color: #999999;">: It is up to the individual theme developers to include the wp_head action in their themes, so there is a chance that your theme will not have it. In that case, check the documentation for your theme to see what action hooks are available to you.</span></p>
<p>That pretty much wraps it up!</p>
<div id="_mcePaste"><strong>Umm, Aren&#8217;t you forgetting something?</strong></div>
<p>By now you may be thinking, &#8220;Great, but what about the Description meta tag you mentioned?&#8221;.</p>
<p>You can use this same technique demonstrated here to create unique page descriptions with just a few minor code changes. Just replace keyword references with description references.</p>
<p>Do you have any suggestions to imporve on this? Find it helpful? Have some else to share? Leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edwardstafford.com/2010/02/04/wordpress-use-custom-fields-to-add-keyword-meta-data-to-your-posts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
