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

Using DeoxIT to repair old game catridges

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

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

Blogs and Friends

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

Blog Activity

Blog Activity