### Install SEO Fields Plugin for Craft CMS Source: https://github.com/studioespresso/craft-seo-fields/blob/develop-v5/README.md Instructions for installing the SEO Fields plugin using Composer and Craft CMS's command-line interface. This process involves navigating to the project directory, requiring the package via Composer, and then installing it within Craft CMS. ```bash cd /path/to/my-craft-project.dev composer require studioespresso/craft-seo-fields ./craft install/plugin seo-fields ``` -------------------------------- ### Install SEO Fields Plugin via Composer and Craft CLI Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Instructions to install the SEO Fields plugin using Composer for dependency management and then enabling it through the Craft CMS command-line interface. ```bash # Navigate to your Craft project directory cd /path/to/my-craft-project # Install the plugin via Composer composer require studioespresso/craft-seo-fields # Install the plugin through Craft's CLI ./craft install/plugin seo-fields ``` -------------------------------- ### Enable per-site robots.txt configuration Source: https://github.com/studioespresso/craft-seo-fields/blob/develop-v5/docs/robots.md To enable site-specific robots.txt files in a multisite Craft installation, add the robotsPerSite key to your configuration file. This allows individual robots.txt management for each site via the Control Panel. ```php return [ "robotsPerSite" => true ]; ``` -------------------------------- ### Render SEO Meta Tags using Template Hook Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Example of integrating the 'seo-fields' template hook into a Craft CMS layout file to automatically render all configured SEO meta tags, Open Graph data, Twitter cards, and Schema.org JSON-LD within the HTML head. ```twig {# In your layout template (e.g., _layouts/base.twig) #}
{# This hook renders all SEO meta tags automatically #} {% hook 'seo-fields' %} {# Your other head content #} {% block content %}{% endblock %} ``` -------------------------------- ### Generate Custom Schema.org JSON-LD Markup in Twig Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Provides examples of creating custom Schema.org markup using the `seoFields` variable in Twig templates. This allows for fine-grained control over structured data, including Organization, Article, and Recipe schemas. The plugin automatically generates Schema.org data, but this method allows for customization. ```twig {# Disable automatic schema rendering #} {% do entry.setShouldRenderSchema(false) %} {# Create custom Schema.org markup using the seoFields variable #} {# @var schema \Spatie\SchemaOrg\Schema #} {% set schema = seoFields.schema %} {# Organization schema example #} {{ schema.organization .name("My Company") .email("info@mycompany.com") .url("https://www.mycompany.com") .logo("https://www.mycompany.com/logo.png") .sameAs([ "https://www.facebook.com/mycompany", "https://twitter.com/mycompany", "https://www.linkedin.com/company/mycompany" ]) |raw }} {# Article schema example #} {{ schema.article .headline(entry.title) .description(entry.excerpt) .author(schema.person.name(entry.author.fullName)) .datePublished(entry.postDate|date('c')) .dateModified(entry.dateUpdated|date('c')) .image(entry.featuredImage.one().url) |raw }} {# Recipe schema example #} {{ schema.recipe .name(entry.title) .description(entry.description) .prepTime("PT" ~ entry.prepTime ~ "M") .cookTime("PT" ~ entry.cookTime ~ "M") .recipeYield(entry.servings ~ " servings") .recipeIngredient(entry.ingredients.all()|map(i => i.name)) |raw }} ``` -------------------------------- ### Initialize and Customize Schema Objects Source: https://github.com/studioespresso/craft-seo-fields/blob/develop-v5/docs/schema.md Demonstrates how to access the schema object via the seoFields variable and chain methods to define properties like organization name and email. The output must be rendered using the raw filter to ensure valid JSON-LD output. ```twig {# @var schema \Spatie\SchemaOrg\Schema #} {% set schema = seoFields.schema %} {{ schema.organization .name("Studio Espresso") .email("info@studioespresso.co") |raw }} ``` -------------------------------- ### DefaultsService API Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Methods for managing site-wide SEO defaults and robots.txt configuration. ```APIDOC ## DefaultsService ### Description Manages site-wide SEO default values and robots.txt content. ### Methods - **getDataBySite($site)**: Gets defaults for the current site. - **getDataBySiteId($siteId)**: Gets defaults by site ID. - **getRobotsForSite($site)**: Retrieves robots.txt content. - **copyDefaultsForSite($newSite, $oldPrimarySiteId)**: Copies defaults to a new site. ### Usage Example ```php $robots = SeoFields::$plugin->defaultsService->getRobotsForSite($site); ``` ``` -------------------------------- ### Manage Site-wide SEO Defaults with DefaultsService Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt The DefaultsService provides access to site-specific SEO configuration, including robots.txt content and default meta values. It also supports cloning defaults between sites. ```php use studioespresso\seofields\SeoFields; $defaults = SeoFields::$plugin->defaultsService->getDataBySite($site); $defaults = SeoFields::$plugin->defaultsService->getDataBySiteId($siteId); $robots = SeoFields::$plugin->defaultsService->getRobotsForSite($site); SeoFields::$plugin->defaultsService->copyDefaultsForSite($newSite, $oldPrimarySiteId); ``` -------------------------------- ### NotFoundService API Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Methods for tracking and managing 404 error records. ```APIDOC ## NotFoundService ### Description The NotFoundService tracks 404 errors and provides methods to manage or clear these records. ### Methods - **markAsHandled($recordId)**: Marks a 404 record as resolved. - **deletetById($id)**: Deletes a specific 404 record. - **deleteAll()**: Deletes all tracked 404 records. ### Usage Example ```php SeoFields::$plugin->notFoundService->markAsHandled($recordId); ``` ``` -------------------------------- ### Manage URL Redirects with RedirectService Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Handles the retrieval, creation, deletion, and bulk importing of URL redirects. Supports both exact and regex matching with configurable HTTP status codes. ```php use studioespresso\seofields\SeoFields; use studioespresso\seofields\models\RedirectModel; $redirects = SeoFields::$plugin->redirectService->getAllRedirects(); $redirect = new RedirectModel(); $redirect->pattern = '/old-page'; $redirect->redirect = 'https://example.com/new-page'; $redirect->method = 301; SeoFields::$plugin->redirectService->saveRedirect($redirect); SeoFields::$plugin->redirectService->deleteRedirectById(123); ``` -------------------------------- ### Configure Craft SEO Fields Plugin in PHP Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Illustrates how to configure the Craft SEO Fields plugin by creating a `config/seo-fields.php` file. This file allows for multi-environment settings and customization of various plugin behaviors, including sidebar labels, title separators, and robot/sitemap configurations. ```php "SEO", // Separator between page title and site name "titleSeperator" => "-", // Enable per-site robots.txt configuration "robotsPerSite" => false, // Enable per-site sitemap configuration (deprecated in 5.1.0) "sitemapPerSite" => true, // Handle of your SEO field (must match exactly) "fieldHandle" => "seo", // Maximum number of 404 records to track "notFoundLimit" => 10000, // Automatically create redirects when entry URLs change "createRedirectForUriChange" => true, // Separate alternate links by site group (for multi-brand sites) "logicallySeperatedSiteGroups" => false, // Additional Schema.org types for section configuration "schemaOptions" => [ get_class(\\Spatie\\SchemaOrg\\Schema::recipe()) => 'Recipe', get_class(\\Spatie\\SchemaOrg\\Schema::event()) => 'Event', get_class(\\Spatie\\SchemaOrg\\Schema::product()) => 'Product', get_class(\\Spatie\\SchemaOrg\\Schema::localBusiness()) => 'Local Business', ], ]; ``` -------------------------------- ### Register Custom Elements for SEO Support Source: https://github.com/studioespresso/craft-seo-fields/blob/develop-v5/docs/templating.md Extends SEO Fields functionality to custom elements by registering them via the EVENT_SEOFIELDS_REGISTER_ELEMENT event in PHP. This allows the plugin to recognize and process SEO data for third-party or custom model classes. ```php Event::on(SeoFields::class, SeoFields::EVENT_SEOFIELDS_REGISTER_ELEMENT, function (RegisterSeoElementEvent $event) use ($elements) { $event->elements = array_merge($event->elements,[ Product::class, ]); } ); ``` -------------------------------- ### SchemaService API Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Methods for accessing Spatie Schema.org integration and generating structured data. ```APIDOC ## SchemaService ### Description Provides access to the Spatie Schema.org library for creating structured data. ### Methods - **getDefaultOptions()**: Returns an array of available Schema.org types. - **schema()**: Returns a new Schema instance. ### Usage Example ```php $schema = SeoFields::$plugin->schemaService->schema(); ``` ``` -------------------------------- ### Register Custom Elements for SEO Support in PHP Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Shows how to register custom element types with the Craft SEO Fields plugin using its event system. This is necessary if you have custom elements that need to leverage the plugin's SEO features. The registration is done within your module or plugin's `init()` method. ```php elements = array_merge($event->elements, [ CustomElement::class, ]); } ); ``` -------------------------------- ### Generate Structured Data with SchemaService Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt The SchemaService integrates with the Spatie Schema.org library to provide structured data capabilities. It allows retrieval of supported schema types and instantiation of new schema objects. ```php use studioespresso\seofields\SeoFields; use Spatie\SchemaOrg\Schema; $options = SeoFields::$plugin->schemaService->getDefaultOptions(); $schema = SeoFields::$plugin->schemaService->schema(); ``` -------------------------------- ### SitemapService API Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Methods for generating XML sitemaps and managing sitemap caches for entries, categories, and products. ```APIDOC ## SitemapService ### Description The SitemapService handles the generation of XML sitemaps and cache management for various content types. ### Methods - **shouldRenderBySiteId($site)**: Checks if the sitemap should render for a specific site. - **getSitemapIndex($data)**: Retrieves the sitemap index XML. - **getSitemapData($siteId, $type, $id)**: Retrieves sitemap data for a specific section, category, or product type. - **clearCaches()**: Clears all sitemap caches. - **clearCacheForElement($element)**: Clears cache for a specific element. ### Usage Example ```php $xml = SeoFields::$plugin->sitemapService->getSitemapData($siteId, 'entry', $sectionId); ``` ``` -------------------------------- ### Custom Meta Template with SEO Fields Twig Function Source: https://context7.com/studioespresso/craft-seo-fields/llms.txt Demonstrates how to use the `getSeoFields()` Twig function to retrieve SEO data and manually construct meta tags, including page title, description, Open Graph, Twitter Cards, robots directives, canonical URLs, hreflang links, and JSON-LD. ```twig {# Custom meta template with full control #} {% set seoFields = getSeoFields() %} {% set element = seoFields.element %} {% set meta = seoFields.meta %} {% if meta and element %} {# Page title with site name #}