-
-
Notifications
You must be signed in to change notification settings - Fork 45
11. Dynamic Keyword Generation
This Craft CMS 2.x plugin is no longer supported, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons.
The Craft CMS 3.x version of this plugin can be found here: craft-seomatic and can also be installed via the Craft Plugin Store in the Craft CP.
Generating good keywords from dynamic data is a pain; to this end, SEOmatic uses the TextRank PHP library to generate high quality keywords from arbitrary text data.
All three of these methods accomplish the same thing:
{# Extract keywords using the 'extractKeywords' function #}
{{ extractKeywords( TEXT, LIMIT ) }}
{# Extract keywords using the 'extractKeywords' filter #}
{{ TEXT | extractKeywords( LIMIT ) }}
{# Extract keywords using the 'extractKeywords' variable #}
{% do craft.seomatic.extractKeywords( TEXT, LIMIT ) %}
TEXT is the text to extract the keywords from, and the optional LIMIT parameter specifies the maximum number of keywords to return (the default is 15).
Here's an example use of the extractKeywords()
function:
{{ extractKeywords('Scientists have developed a gel that helps brains
recover from traumatic injuries. It has the potential to treat head injuries
suffered in combat, car accidents, falls, or gunshot wounds. Developed by
Dr. Ning Zhang at Clemson University in South Carolina, the gel is injected
in liquid form at the site of injury and stimulates the growth of stem cells
there. Brain injuries are particularly hard to repair, since injured tissues
swell up and can cause additional damage to the cells. So far, treatments
have tried to limit this secondary damage by lowering the temperature or
relieving the pressure at the site of injury. However, these techniques are
often not very effective. More recently, scientists have considered
transplanting donor brain cells into the wound to repair damaged tissue.
This method has so far had limited results when treating brain injuries. The
donor cells often fail to grow or stimulate repair at the injury site,
possibly because of the inflammation and scarring present there. The injury
site also typically has very limited blood supply and connective tissue,
which might prevent donor cells from getting the nutrients they require. Dr.
Zhangs gel, however, can be loaded with different chemicals to stimulate
various biological processes at the site of injury. In previous research
done on rats, she was able to use the gel to help re-establish full blood
supply at the site of brain injury. This could help create a better
environment for donor cells. In a follow-up study, Dr. Zhang loaded the gel
with immature stem cells, as well as the chemicals they needed to develop
into full-fledged adult brain cells. When rats with severe brain injuries
were treated with this mixture for eight weeks, they showed signs of
significant recovery. The new gel could treat patients at varying stages
following injury, and is expected to be ready for testing in humans in about
three years.') }}
This will output the following:
injury site, brain cells, brain injuries, donor cells, donor brain cells,
injury, site of injury, site, cells, brain, injuries, repair, donor, damage
to the cells, blood
So tying it all together, you might do something like this for a dynamic Blog entry:
{% set seomaticMeta = {
seoTitle: entry.title,
seoDescription: entry.summary,
seoKeywords: extractKeywords(entry.blog),
seoImage: entry.image.url,
canonicalUrl: seomaticMeta.canonicalUrl,
twitter: {
card: seomaticMeta.twitter.card,
site: seomaticMeta.twitter.site,
creator: seomaticMeta.twitter.creator,
title: entry.title,
description: entry.summary,
image: entry.image.url
},
og: {
type: seomaticMeta.og.type,
locale: seomaticMeta.og.locale,
url: entry.url,
title: entry.title,
description: entry.summary,
image: entry.image.url,
site_name: seomaticMeta.og.site_name,
see_also: seomaticMeta.og.see_also
}
} %}
And there you have it, dynamic keywords for your SEO Meta. Note that we set the canonicalUrl
to seomaticMeta.canonicalUrl
, effectively leaving it unchanged.
Anywhere we are setting a field to seomaticMeta.*
, we're setting it to what it already is, essentially saying to leave it unchanged. We do this because Twig requires that you pass in the entire array to the set
operator.