-
-
Notifications
You must be signed in to change notification settings - Fork 45
15. Utility Filters & Functions
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.
SEOmatic exposes a few useful utility filters & functions that you can use... or not.
All three of these methods accomplish the same thing:
{# Truncate a string on word boundaries using the 'truncateStringOnWord' function #}
{{ truncateStringOnWord( THESTRING, DESIREDLENGTH ) }}
{# Truncate a string on word boundaries using the 'truncateStringOnWord' filter #}
{{ THESTRING | truncateStringOnWord( DESIREDLENGTH ) }}
{# Truncate a string on word boundaries using the 'truncateStringOnWord' variable #}
{% do craft.seomatic.truncateStringOnWord( THESTRING, DESIREDLENGTH ) %}
THESTRING is the string to be truncated, and the optional DESIREDLENGTH parameter specifies the desired length in characters. The returned string will be broken on a whole-word boundary, with an … appended to the end if it is truncated.
You shouldn't need to use truncateStringOnWord() for SEO Meta like seoTitle
& seoDescription
that have character limitations, because SEOmatic will truncate them for you automatically. However you may find this function handy for other purposes.
All three of these methods accomplish the same thing:
{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' function #}
{{ extractTextFromMatrix( THEMATRIXBLOCK ) }}
{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' filter #}
{{ THEMATRIXBLOCK | extractTextFromMatrix() }}
{# Extract all and concatenate all of the text fields from a Matrix block using the 'extractTextFromMatrix' variable #}
{% do craft.seomatic. extractTextFromMatrix( THEMATRIXBLOCK ) %}
THEMATRIXBLOCK is the Matrix block to extract text from. It iterates through all of the 'Text' and 'Rich Text' fields in a Matrix block, and concatenates the text together for you. This is a useful precursor for the extractKeywords()
function.
All three of these methods accomplish the same thing:
{# Ordinal-encode an email address to obfuscate it using the 'encodeEmailAddress' function #}
{{ encodeEmailAddress( EMAILADDRESS ) }}
{# Ordinal-encode an email address to obfuscate it using the 'encodeEmailAddress' filter #}
{{ EMAILADDRESS | encodeEmailAddress() }}
{# Ordinal-encode an email address to obfuscate it using the 'encodeEmailAddress' variable #}
{% do craft.seomatic.encodeEmailAddress( EMAILADDRESS ) %}
EMAILADDRESS is the email address to be ordinal-encoded. For instance, [email protected]
becomes:
info@nystudio107.com
Google can still properly decode email addresses that are ordinal-encoded, it's still readable by humans when displayed, but it prevents some bots from recognizing it as an email address.
Returns an array of localized URLs for the current page request. This handles elements with localized slugs, etc. This function returns the unique URLs for each language for the current page request, not just the localized site URLs.
Both of these methods accomplish the same thing:
{# Get an array of localized URLs for the current request using the 'getLocalizedUrls' function #}
{% set myLocalizedUrls = getLocalizedUrls() }}
{# Get an array of localized URLs for the current request using the 'getLocalizedUrls' variable #}
{% set myLocalizedUrls = craft.seomatic.getLocalizedUrls() %}
You'll be returned an array that looks like this:
{
'en': 'http://nystudio107.dev/',
'el_gr': 'http://nystudio107.dev/gr/'
}
With a key/value pair for each language your site is localized in, in the order you have them set in the AdminCP. This makes it very easy to create a "language switcher" menu.
All three of these methods accomplish the same thing:
{# Get a fully qualified URL based on the siteUrl using the 'getFullyQualifiedUrl' function #}
{{ getFullyQualifiedUrl( URLPATH ) }}
{# Get a fully qualified URL based on the siteUrl using the 'getFullyQualifiedUrl' filter #}
{{ URLPATH | getFullyQualifiedUrl }}
{# Get a fully qualified URL based on the siteUrl using the 'getFullyQualifiedUrl' variable #}
{% do craft.seomatic.getFullyQualifiedUrl( URLPATH ) %}
URLPATH is either a URL (in which case it is just returned) or a path, in which case it is combined with the siteUrl
and returned.