kodansha/wack-preview
Composer 安装命令:
composer require kodansha/wack-preview
包简介
Helpers to make it possible to preview posts on frontend.
关键字:
README 文档
README
WACK Preview is a WordPress plugin that helps you to implement a preview system for unpublished drafts on your frontend application.
How it works
Originally designed for use with the WACK Stack, this plugin can also be used with other decoupled, headless WordPress setups that use separate frontend applications, such as those utilizing the REST API or WPGraphQL.
This plugin replaces the default permalinks and preview links for posts (and other custom post types) generated by WordPress.
Preview links are generated with a JWT token as a parameter. By verifying this JWT using the same secret key on your frontend application, you can ensure the token was legitimately generated by WordPress. This lets you securely fetch data before it is published.
Installation
- Requires PHP 8.2 or later
- Requires WordPress 6.9 or later
- Requires Composer
Using Composer
composer require kodansha/wack-preview
Note
This plugin is not available on the WordPress.org plugin repository. The only installation method currently available is through Composer.
Configuration
To use WACK Preview, you need to configure URL mappings for displaying pages on the frontend. Additionally, you must set up a secret token for preview URLs.
You can configure these settings using one of the following methods:
- Define settings with the
WACK_PREVIEW_SETTINGSconstant - Configure settings through the WordPress admin menu
Setting via WACK_PREVIEW_SETTINGS constant
Define WACK_PREVIEW_SETTINGS in functions.php or similar:
define('WACK_PREVIEW_SETTINGS', [ // URL of frontend 'frontend_base_url' => 'https://frontend.example.com', // Preview token settings 'preview_token' => [ // Secret key for Token generation 'secret_key' => 'THIS_IS_A_SECRET_KEY', // Expiry time of token (seconds) 'expiry_time' => 60 * 60 * 24 ], // Path mappings for publish and preview links // %id% will be replaced with the actual post id when generating links // e.g. /post/%id% -> /post/123 // The generated links will replace the original links 'path_mappings' => [ // Mapping for each post types // This example is for the default "post" type 'post' => [ 'publish' => '/posts/%id%', 'preview' => '/posts/preview/%id%' ], // Another custom post type you would like to rewrite permalinks // %slug% will be replaced with the actual post slug (name) when generating links // e.g. /news/%slug% -> /news/an-awesome-news // Note that the post slug may not be set in some cases. In such cases, the post ID will be automatically used as a fallback. 'news' => [ 'publish' => '/news/%slug%', // You can set a preview path including query string // query strings will be merged with "preview_token" and "preview" 'preview' => '/news/preview?slug=%slug%' ], ], ]);
Note
The secret_key is used to sign tokens with HMAC-SHA256 (HS256). A minimum
length of 32 bytes is required. Using a shorter key will result in a fatal
error at runtime. Generate a sufficiently long random value with a command
such as openssl rand -hex 32.
Note
On multisite WordPress installations, the WACK_PREVIEW_SETTINGS constant does not function as expected. In such cases, configure settings individually for each site using the admin menu.
Setting via WordPress admin menu
Navigate to the WACK Preview settings screen in the WordPress admin menu and follow the on-screen instructions.
Tip
If settings are defined in both the WACK_PREVIEW_SETTINGS constant and the
admin menu, the constant's settings will take precedence. This allows flexible
configuration - for example, you could define only the secret_key as a
constant (keeping it out of the database) while managing other settings
through the admin screen.
Preview token payload
The preview token is a JWT token that contains the following payload:
{
// Post ID (or post slug if "%slug%" template is set in the path mappings)
// Note: If the post does not have the slug, it will fallback to the post ID even if the "%slug%" template is set.
"sub": 123,
// Issuer (always set to `wack-preview`)
"iss": "wack-preview",
// Issued at (UNIX timestamp)
"iat": 1630000000,
// Expiry time (UNIX timestamp)
"exp": 1630003600
}
This means that the frontend application can verify the token is valid for the
specified post by checking the sub field.
API
wack_preview_verify_token
function wack_preview_verify_token(string $preview_token): bool
This is a utility function provided as a convenient API. It can be used in WordPress themes to verify preview tokens and control access to unpublished posts.
It returns true if the token is valid, false otherwise.
kodansha/wack-preview 适用场景与选型建议
kodansha/wack-preview 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 409 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kodansha/wack-preview 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kodansha/wack-preview 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kodansha/wack-preview 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Plugin for YOURLS. Default tools to use in some laemmi plugins
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Allows polling user for event dates and times.
Support swipe touch gestures to navigate in paginated lists and on Tidypics image pages.
Add a postscript to every email notification sent out to users.
统计信息
- 总下载量: 409
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-03-12