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.

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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Coherence and weblogic.xml in different types of J2EE web app deployments

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 1 - introduction and case disassembly

jPhotoFrame updated to version 0.3.1 with an image rotation correction utility

iOS 11 pre-GM mini review before it gets revealed next week

Why you should never use Java enums as keys with Oracle Coherence caches

Hacking the VideoPak 7inch promotional brochure to work as a digital photo frame

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity