whitespace-se / wordpress-plugin-gatsby
为 Gatsby 添加预览和其他集成功能
v1.0.0
2022-01-25 13:29 UTC
Requires
- composer/installers: ~1.0
Requires (Dev)
- ivome/graphql-relay-php: 0.3.1
- webonyx/graphql-php: 14.3.0
README
WordPress 插件,用于添加预览和其他与 Gatsby 的集成功能。
如何安装
如果您想将此插件作为 MU 插件使用,首先将以下内容添加到您的 composer.json 文件中
{ "extra": { "installer-paths": { "path/to/your/mu-plugins/{$name}/": [ "whitespace-se/wordpress-plugin-gatsby" ] } } }
其中 path/to/your/mu-plugins
是类似于 wp-content/mu-plugins
或 web/app/mu-plugins
的路径。
然后通过 composer 获取插件
composer require whitespace-se/wordpress-plugin-gatsby
配置
预览
在您的配置中(例如在 wp-config.php
中)定义 GATSBY_PREVIEW_ENDPOINT
常量以设置预览,例如
define("GATSBY_PREVIEW_ENDPOINT", "https://example.com/wp-preview");
此 URL 将附加一个包含以下参数的查询字符串
id
– 帖子的 GraphQL IDuser
=> 预览帖子的用户的加密 IDwpnonce
=> 一个wp_rest
noncecontentType
=> 帖子的内容类型(已弃用)
您应该使用 GraphQL 来请求预览数据,并且该请求必须包含包含查询字符串中接收到的值的 x-wp-user
和 x-wp-nonce
标头。
GraphQL 查询可能如下所示,其中 $id
是在查询字符串中接收到的帖子的 GraphQL ID
query PreviewQuery($id: ID!) { wp { contentNode(id: $id, asPreview: true) { # ... } } }
保存时刷新
如果您需要在帖子更新时触发 Gatsby 刷新,请定义 GATSBY_REFRESH_ENDPOINTS
常量(例如在 wp-config.php
中)。示例
define("GATSBY_REFRESH_ENDPOINTS", "https://:8000/__refresh");
值可以是单个 URL 或多个 URL,可以是数组或单个字符串中的逗号分隔。您还可以使用 gatsby_refresh_endpoints
过滤器来修改值。