Before we go into details of my solution, if you're looking for website hosting or are thinking of switching hosts to get a better deal, make sure to go to Hosting Foundry to find the best web hosting service for your website, it can save you big time!
My solution depends on two things to hold true for a blog entry: 1) the first paragraph is sufficient to generate the entire description meta tag, and 2) the tags/categories for a blog entry are sufficient to generate the keywords meta tag.
The result is something like this (using this post as an example):
So lets see how to do this. First the fp-includes/core/core.entry.php file needs to be updated. Specifically the entry_parse() function. My new code is added just before the last two lines of this method, as shown below. What the code does is look for the first new line character in the entry content, then it pulls out the substring from the beginning of the content up to the new line i.e. the first paragraph. After this the bbcode and any HTML tags are stripped out. The final string is then set as the meta_description array element for the entry.
fp-includes/core/core.entry.php
function entry_parse($id, $raw=false) {
...
// --- insert this code to generate meta description
$cut = strpos($arr['content'], "\n");
$desc = preg_replace('#\[[^\]]+\]#', '', substr($arr['content'], 0, $cut - 1));
$desc = preg_replace('#\<[^\]]+\>#', '', $desc);
$arr['meta_description'] = $desc;
// --- insert this code to generate meta description
if ($raw) return $arr;
return $arr;
}
Now that the meta description is generated, it has to be added to the theme template. I added this to my header.tpl file like this:
header.tpl
...
{if $meta_description}
<meta name="description" content="{$meta_description}">
{/if}
{if $categories}
<meta name="keywords" content="{$categories|@filed:false}">
{/if}
...
There is a small catch here. Since this information is generated for an entry only, it will only work inside of an entry block in the template. So my header.tpl file is included like this this:
Template
...
{entry_block}
{entry}
{include file='header.tpl'}
{include file='entry-default.tpl'}
...
This can be done in the single.tpl or comments.tpl files (if comments are enabled).
The bonus of this approach is that the meta tags are not generated for pages that are not a blog entry page.
The same approach can be followed for static pages. In this case the core.static.php file needs to be updated by modifying the static_parse() function in a similar fashion to the code above.
-i