How do I get the meta tags used in SEOmatic for a specific entry and returning it through the Element API?
Not sure how the PHP code looks like when using SEOmatic's PHP API.
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.Sign up to join this community
SEOmatic has some functions that it uses for its controller endpoints for returning metadata for SPAs and the like:
We can leverage these for our own purposes in an Element API endpoint, if for some reason you can't ping the SEOmatic controller via AJAX or use GraphQL to access the data.
use nystudio107\seomatic\helpers\Container as ContainerHelper; use nystudio107\seomatic\models\MetaJsonLdContainer; use nystudio107\seomatic\models\MetaLinkContainer; use nystudio107\seomatic\models\MetaScriptContainer; use nystudio107\seomatic\models\MetaTitleContainer; use nystudio107\seomatic\models\MetaTagContainer; ... $result = ContainerHelper::getContainerArrays( [ MetaTitleContainer::CONTAINER_TYPE, MetaTagContainer::CONTAINER_TYPE, MetaLinkContainer::CONTAINER_TYPE, MetaScriptContainer::CONTAINER_TYPE, MetaJsonLdContainer::CONTAINER_TYPE, ], $uri, $siteId, true );
You'll get an array of the container information back, and you can do whatever you like with it.