nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
165 stars 69 forks source link

Use entry variables in meta tags #1459

Closed philipschilling closed 5 months ago

philipschilling commented 5 months ago

Question

I would like to include an entry variable in the meta description. Specifically, I have stored in a field of an entry a price which I would like to output in the meta description which is a field of the same entry type. The issue is that the meta tags are included dynamically into the pages and I cannot edit them in templates.

I found this solution which looked promising: https://craftcms.stackexchange.com/questions/39543/can-i-define-and-use-global-variables-within-content-fields

Global Variables

I added the global variable {rentalTerms.rentalMinimumPeriod} {rentalTerms:rentalMinimumPeriod} just to see what happens but the output is (rentalTerms.rentalMinimumPeriod) (rentalTerms:rentalMinimumPeriod). So, I didn't bother to try with entry variables.

Replace Function

I added the following function directly after the <head> tag in my template:

{% set rentalPrice = entry.rentalPrice %}
{{ entry.metaDescription|replace({'[rentalPrice]': rentalPrice}) }}

I added the placeholder [rentalPrice] to my Meta Description field:

Screenshot 2024-04-16 at 09 15 24

It is replacing the variable but outputs the text directly after the <body> tag:

Screenshot 2024-04-16 at 09 07 51

In the meta description tag the variable has not been replaced:

Screenshot 2024-04-16 at 09 08 04

Do you have a solution?

khalwat commented 5 months ago

So the way you will want to do this will be using the SEOmatic APIs... something like this:

{% set rentalPrice = entry.rentalPrice %}
{% do seomatic.meta.seoDescription(entry.metaDescription|replace({'[rentalPrice]': rentalPrice})) %}

See here for details: https://nystudio107.com/docs/seomatic/using/#seomatic-variables

Also you generally don't need a separate metaDescription field in Craft, typically you can set up content mapping in SEOmatic -> Content SEO as discussed here: https://nystudio107.com/docs/seomatic/overview.html#the-meta-cascade

Or if you really do need a per-entry custom SEO overrides, you can use an SEO Settings field so things will work more automatically for you: https://nystudio107.com/docs/seomatic/fields.html

philipschilling commented 5 months ago

Thank you, Andrew. It works and was exactly what I was looking for. I read earlier the pages of the last 2 links. I decided to create and use separate meta fields for 2 reasons assuming that SEOmatic stores its data separate from or different to Craft CMS:

  1. If the plugin is discontinued or I decide to uninstall it, I would like to have saved the meta information with my entries.
  2. It is probably easier to handle exports of entries which include meta data.

Honestly, I don't know how SEOmatic technically works and where it stores the data. I just didn't want to loose it or re-enter it when the plugin changes (bug, discontinuation, price increase, etc.).

khalwat commented 5 months ago

Okay, sounds good. Generally you don't need a separate meta description, as pulling from existing content via Content SEO usually works better than manually written descriptions/titles, because they reflect the content on-page.