Igor's Blog

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="[email protected]: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

, , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

WebLogic package-name element classpath generator script

Using Jersey 2.x as a shared library on WebLogic 12.1.2

Google DFP with AdSense fallback is causing infinite scroll pages to go haywire

Workaround for Mobile Safari scrollTop() not updating during scroll

Skipping execution of Maven plugins that do not have a native skip option

Unmarshalling an XML fragment representing a JAXB object without XmlRootElement

Injecting a ContainerRequestContext into a Jersey entity provider class

Obfuscating PHP source code with Maven and YAK Pro PO

Making use of corner shelves with easy to make slide out draws

Octotree breaks GitLab Issue Boards

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again