承接 init-development-studios/headless-wordpress-sync 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. Setting up WordPress
  2. Setting up Laravel

Headless WordPress

Setting up WordPress involves the installation and activation of two WordPress plugins.

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 via HEADLESS_WP_URL in the applications .env file.
  • username - configurable via HEADLESS_WP_USERNAME in the applications .env file.
  • password - configurable via HEADLESS_WP_PASSWORD in 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 init-development-studios/headless-wordpress-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-21