init-development-studios/headless-wordpress-sync
Composer 安装命令:
composer require init-development-studios/headless-wordpress-sync
包简介
Sync content from a headless WordPress instance using the WP REST API.
关键字:
README 文档
README
A Laravel package which simplifies the integration with a headless WordPress instance.
[[TOC]]
Getting Started
This guide will walk though the setting up both the Laravel instance along with headless WordPress instance.
The setup has two steps:
- Setting up WordPress
- Setting up Laravel
Headless WordPress
Setting up WordPress involves the installation and activation of two WordPress plugins.
- CollabPay Headless WordPress Plugin (https://gitlab.com/collabpay/headless-wordpress-plugin) - TODO move this under Init Dev banner for re-usability.
- JWT Authentication For WP REST API (https://en-au.wordpress.org/plugins/jwt-authentication-for-wp-rest-api/) - Third party plugin which enables access to API resources requiring authentication (eg user details).
- Make sure to set WP to use postname for the permalink or the api will just 404.
Step X
Let's kick off by installation the JWT Authentication For WP REST API plugin.
Follow the instructions found on the plugins installation page.
For the vast majority the "Using The WordPress Dashboard" will suffice.
Step X
The JTW Authentication plugin also requires a little configuration. We must define a secret key in the wp-config.php file.
Simply add the following.
define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');
You can use a string from here (https://api.wordpress.org/secret-key/1.1/salt/).
Step X
Activate the JWT Authentication plugin from the Installed Plugins page within the WordPress Admin interface.
Step X
Now let's install the Laravel Headless WordPress Plugin.
As this plugin is not published publicly we're best off installing the new plugin via a ZIP file.
Download the latest version (.zip)
Step X
From the Add New plugin page click the Upload Plugin button found at the top of the page.
Then the Browse button.
Find and select the ZIP file downloaded above.
Click Install Now.
Then click Activate Plugin.
WordPress is now setup!
Laravel
We're now up to installing and configuring the Laravel package.
Step X
The Laravel package can be installed via composer.
However, as this package is not publicly available the following manual changes to the composer.json file are required.
Add the following root json object:
"repositories": [
{
"type": "git",
"url": "git@gitlab.com:init-dev/headless-wordpress/headless-wordpress-sync.git"
}
],
Then run:
composer require init-development-studios/headless-wordpress-sync:dev-main
Note, you must have authorisation to access the GIT repository.
Step X
Several tables require migration.
Publish the migrations.
php artisan vendor:publish --tag=headless-wordpress-sync-migrations
Step X
Run the migration.
php artisan migrate
Step X
Now let's publish the config file.
php artisan vendor:publish --tag=headless-wordpress-sync-config
Step X
There will now be a headless-wordpress-sync.php config file within the Laravel applications config directory.
Take a look over the file and configure your application as desired. Note that the following attributes are required:
url- configurable viaHEADLESS_WP_URLin the applications .env file.username- configurable viaHEADLESS_WP_USERNAMEin the applications .env file.password- configurable viaHEADLESS_WP_PASSWORDin the applications .env file.
Example .env
HEADLESS_WP_URL=https://wordpress.test
HEADLESS_WP_USERNAME=api-user
HEADLESS_WP_PASSWORD="tQdVIc!gdb7eD(o9IZ&^WEXm"
Optional
When developing locally it's often useful to not require SSL / TLS verification.
HEADLESS_WP_VERIFY_CERTIFICATE=false
When hosting content for many sites, providing the website_slug for the site who's content is to be displayed on this Laravel applications instance is required.
This slug matches that defined Posts > Website from withing the Headless WordPress applications Admin interface.
HEADLESS_WP_WEBSITE_SLUG=collabpay
Step X
In order to keep the Laravel application in sync with the WordPress application a Scheduled Task is performed each minute.
The Scheduled Task will run automatically.
Make sure that the application has Scheduled Tasks running and that HEADLESS_WP_SYNC_ENABLED is either absent or set to TRUE in the .env file.
Note that a sync can also be manually performed by running php artisan headless-wp:sync.
Step X
You can now start updating your application to present your WordPress managed content.
Fetching Content
Content can be fetched using standard Eloquent queries!
Posts
All Posts
WpPost::all()
Individual Post
WpPost::whereSlug('individual-post-slug')->first();
Scoped to the current website
WpPost::currentWebsite()->get()
Published posts only
WpPost::published()->get()
Ordering by date / time published
WpPost::orderByDesc('published_at_utc')->get()
In a specified category
WpPost::inCategory(WpCategory::whereSlug('tutorials')->first())->get()
Individual post categories
WpPost::whereSlug('individual-post-slug')->first()->categories;
Individual post tags
WpPost::whereSlug('individual-post-slug')->first()->tags;
Individual post author
WpPost::whereSlug('individual-post-slug')->first()->author;
Related posts
All related and inversely related posts.
To relate a posts add the a Custom Field on a post with the Name related_post_slugs and then set the Value to a comma separated list of post slugs.
WpPost::whereSlug('individual-post-slug')->first()->related();
Related posts query
Same as above but will return a query builder to permit further query building
$query = WpPost::whereSlug('individual-post-slug')->first()->relatedQuery();
// Can then be queried further (eg only return published related articles)
$query->published()->get();
Search posts
Search for an exact term within a posts title and content.
Note this is performed at the DB level.
For better search consider using Laravel Scout.
WpPost::search('individual-post-slug')->first()->related();
Categories
All categories
WpCategory::all()
Individual category
WpCategory::whereSlug('tutorials')->first()
Authors (users)
All authors
WpUser::all();
Individual author
WpUser::find(1);
Individual authors Gravatar avatar url
WpUser::find(1)->image()->url();
TODOs
- Disable WP frontend such that it's truly headless. Used another plugin in the past however having this built in would be ideal.
- Search input rendering on individual category pages
- Improved social media meta tags
- Idea: add search to sidebar on individual posts and possibly other places throughout the site. Easy to do now.
Support
TODO
Roadmap
Short term
- Make WP plugin public. Requires further refining and testing. Likely achievable within CollabPay use case.
- Make Laravel plugin public. Should only occur inline with WP plugin being published publicly.
- Add screenshots to readme
Longer term
- Expand support for more WordPress features / functionality.
- Support for Laravel Scout when searching posts.
Contributing
TODO
Authors and acknowledgment
TODO
License
TODO
init-development-studios/headless-wordpress-sync 适用场景与选型建议
init-development-studios/headless-wordpress-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.78k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「wordpress」 「headless」 「laravel」 「Init Development Stuidios」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 init-development-studios/headless-wordpress-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 init-development-studios/headless-wordpress-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 init-development-studios/headless-wordpress-sync 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
Get wordpress nav menus and share the main site menus with the child sites.
统计信息
- 总下载量: 5.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-21