Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

Meta tags are still very important for SEO (Search Engine Optimisation) and posting to social media. For example if you don't have the description meta tag for your blog post, Facebook doesn't show the page description. This makes these tags crucial for any blog. If you use FlatPress, there is the SEO Metatag Info available to generate these tags for you automatically, however, recently I've noticed that there is a security issue with this plugin which allows anyone to write arbitrary files to your server and I quickly disabled the plugin and came up with an alternative to generating the meta tags I needed.

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):
seometa.png


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

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.