项目作者: ashhitch

项目描述 :
Gatsby Plugin to Bring Yoast Seo Data into Gatsby via WpGraphQL.
高级语言: TypeScript
项目地址: git://github.com/ashhitch/gatsby-plugin-wpgraphql-seo.git
创建时间: 2020-10-17T16:14:27Z
项目社区:https://github.com/ashhitch/gatsby-plugin-wpgraphql-seo

开源协议:MIT License

下载


Gatsby SEO For WpGraphQL and Yoast

npm

Takes data from WpGraphQL and WPGraphQl Yoast SEO and provides you with Meta Tags and JSON+LD Schema in Gatsby.

Basic Setup

Install package

Yarn or NPM install

  1. yarn add gatsby-plugin-wpgraphql-seo

or

  1. npm install gatsby-plugin-wpgraphql-seo

Also, if you haven’t already, install gatsby-plugin-react-helmet and react-helmet

  1. yarn add gatsby-plugin-react-helmet react-helmet

or

  1. npm install gatsby-plugin-react-helmet react-helmet

Find this useful?

Buy Me A Coffee

Setup Gatsby

Add gatsby-plugin-react-helmet to your plugins in gatsby-config, alongside any others you alrady have:

  1. plugins: ['gatsby-plugin-react-helmet']

In your sites layout setup the context provider to pass the component your general site settings.

  1. import React, { useState } from 'react';
  2. import { useStaticQuery, graphql } from 'gatsby';
  3. import { SEOContext } from 'gatsby-plugin-wpgraphql-seo';
  4. export const Layout = () => {
  5. const {
  6. wp: { seo },
  7. } = useStaticQuery(graphql`
  8. query SiteInfoQuery {
  9. wp {
  10. seo {
  11. contentTypes {
  12. post {
  13. title
  14. schemaType
  15. metaRobotsNoindex
  16. metaDesc
  17. }
  18. page {
  19. metaDesc
  20. metaRobotsNoindex
  21. schemaType
  22. title
  23. }
  24. }
  25. webmaster {
  26. googleVerify
  27. yandexVerify
  28. msVerify
  29. baiduVerify
  30. }
  31. schema {
  32. companyName
  33. personName
  34. companyOrPerson
  35. wordpressSiteName
  36. siteUrl
  37. siteName
  38. inLanguage
  39. logo {
  40. sourceUrl
  41. mediaItemUrl
  42. altText
  43. }
  44. }
  45. social {
  46. facebook {
  47. url
  48. defaultImage {
  49. sourceUrl
  50. mediaItemUrl
  51. }
  52. }
  53. instagram {
  54. url
  55. }
  56. linkedIn {
  57. url
  58. }
  59. mySpace {
  60. url
  61. }
  62. pinterest {
  63. url
  64. metaTag
  65. }
  66. twitter {
  67. username
  68. cardType
  69. }
  70. wikipedia {
  71. url
  72. }
  73. youTube {
  74. url
  75. }
  76. }
  77. }
  78. }
  79. }
  80. `);
  81. return (
  82. <SEOContext.Provider value={{ global: seo }}>
  83. <p>... your layout</p>
  84. </SEOContext.Provider>
  85. );
  86. };

Optionally you can pass options to the context via the options prop.

  1. const options = {
  2. schemaReplacement: {
  3. from: 'EXAMPLE';
  4. to: 'TO_REPLACE';
  5. }
  6. }
  7. return (
  8. <SEOContext.Provider value={{ global: seo, options }}>
  9. <p>... your layout</p>
  10. </SEOContext.Provider>
  11. );

Currently this only supports the schemaReplacement option. This will replace the from value with the to value in the JSON+LD Schema.

For each page or template you then need to add the SEO Component

  1. import React from 'react';
  2. import { graphql } from 'gatsby';
  3. import Seo from 'gatsby-plugin-wpgraphql-seo';
  4. const Page = ({ data: { wpPage } }) => {
  5. return (
  6. <>
  7. <Seo post={wpPage} ></Seo>
  8. <p>Rest of page</p>
  9. </>
  10. );
  11. };
  12. export default Page;
  13. export const pageQuery = graphql`
  14. query GET_PAGE($id: String!) {
  15. wpPage(id: { eq: $id }) {
  16. nodeType
  17. title
  18. uri
  19. seo {
  20. title
  21. metaDesc
  22. focuskw
  23. metaKeywords
  24. metaRobotsNoindex
  25. metaRobotsNofollow
  26. opengraphTitle
  27. opengraphDescription
  28. opengraphImage {
  29. altText
  30. sourceUrl
  31. srcSet
  32. }
  33. twitterTitle
  34. twitterDescription
  35. twitterImage {
  36. altText
  37. sourceUrl
  38. srcSet
  39. }
  40. canonical
  41. cornerstone
  42. schema {
  43. articleType
  44. pageType
  45. raw
  46. }
  47. }
  48. }
  49. }
  50. `;

For archive pages

  1. import React from 'react';
  2. import { graphql } from 'gatsby';
  3. import Seo from 'gatsby-plugin-wpgraphql-seo';
  4. const Blog = ({ data }) => {
  5. return (
  6. <>
  7. <Seo
  8. title="Blog Title"
  9. postSchema={JSON.parse(
  10. data.wp.seo.contentTypes.post.schema.raw
  11. )}
  12. ></Seo>
  13. <p>Rest of page</p>
  14. </>
  15. );
  16. };
  17. export default Blog;
  18. export const pageQuery = graphql`
  19. query GET_POSTS($ids: [String]) {
  20. wp {
  21. seo {
  22. contentTypes {
  23. post {
  24. schema {
  25. raw
  26. }
  27. }
  28. }
  29. }
  30. }
  31. allWpPost(filter: { id: { in: $ids } }) {
  32. nodes {
  33. ...
  34. }
  35. }
  36. }
  37. `;

Additional props are provided for overrides and simpler pages:

  1. title: String to override Title
  2. meta: Array of key value objects for meta tags (e.g property, content)
  3. post: WpGrahpQL post object
  4. postSchema: JSON object to replace complete JSON+LD schema;

Removing search action from schema.

By default Yoast adds a search action to the schema if you want remove it you can add the following PHP to your functions.php file:

  1. <?php
  2. add_filter('wpseo_schema_website', 'XX_remove_schema_search');
  3. function XX_remove_schema_search($data)
  4. {
  5. if ($data['potentialAction']) {
  6. foreach ($data['potentialAction'] as $key => $value) {
  7. if ($value['@type'] && $value['@type'] == 'SearchAction') {
  8. unset($data['potentialAction'][$key]);
  9. }
  10. }
  11. }
  12. return $data;
  13. }

… More docs coming soon