承接 boxybird/inertia-wordpress 相关项目开发

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

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

boxybird/inertia-wordpress

Composer 安装命令:

composer require boxybird/inertia-wordpress

包简介

The WordPress adapter for Inertia.js

README 文档

README

The unofficial Inertia.js server-side adapter for WordPress.

Installation

Option 1: Install the package via composer. (Recommended)

composer require boxybird/inertia-wordpress

Option 2: Clone or download as a plugin and run composer install before activating in WordPress Admin.

Bare-bones Example Theme

Example Movie CPT WordPress Project

Inertia Docs

Root Template Example

Location: /wp-content/themes/your-theme/app.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <?php wp_head(); ?>
    </head>
    <body>

        <?php bb_inject_inertia(); ?> // Adds Inertia to the page

        <?php wp_footer(); ?>
    </body>
</html>

Root Template File Override

Location: /wp-content/themes/your-theme/functions.php

By default the WordPress adapter will use the app.php from .../your-theme/app.php. If you would like to use a different file name, you can change it. E.g. .../your-theme/layout.php.

<?php

add_action('init', function () {
    Inertia::setRootView('layout.php');
});

Inertia Function Output Override

By default the bb_inject_inertia() function returns <div id="app" data-page="{...inertiaJsonData}"></div>. If you need to override the div id, you can.

// Override 'id="app"' to 'id="my_app"' and add classes
<?php bb_inject_inertia('my_app', 'bg-blue-100 font-mono p-4'); ?>

Inertia Response Examples

Basic

Location: /wp-content/themes/your-theme/index.php

<?php

use BoxyBird\Inertia\Inertia;

global $wp_query;

Inertia::render('Index', [
    'posts' => $wp_query->posts,
]);

Less Basic

Location: /wp-content/themes/your-theme/index.php

This may look busy, however it can be thought of as a "Controller". It gives you a place to handle all your business logic. Leaving your Javacript files easier to reason about.

<?php

use BoxyBird\Inertia\Inertia;

global $wp_query;

// Build $posts array
$posts = array_map(function ($post) {
    return [
        'id'      => $post->ID,
        'title'   => get_the_title($post->ID),
        'link'    => get_the_permalink($post->ID),
        'image'   => get_the_post_thumbnail_url($post->ID),
        'content' => apply_filters('the_content', get_the_content(null, false, $post->ID)),
    ];
}, $wp_query->posts);

// Build $pagination array
$current_page = isset($wp_query->query['paged']) ? (int) $wp_query->query['paged'] : 1;
$prev_page    = $current_page > 1 ? $current_page - 1 : false;
$next_page    = $current_page + 1;

$pagination = [
    'prev_page'    => $prev_page,
    'next_page'    => $next_page,
    'current_page' => $current_page,
    'total_pages'  => $wp_query->max_num_pages,
    'total_posts'  => (int) $wp_query->found_posts,
];

// Return Inertia view with data
Inertia::render('Posts/Index', [
    'posts'      => $posts,
    'pagination' => $pagination,
]);

Quick Note

You may be wondering what this moster line above does:

'content' => apply_filters('the_content', get_the_content(null, false, $post->ID));

Because we can't use the WordPress function the_content() outside of a traditional theme template setup, we need to use get_the_content() instead. However, we first need to apply the filters other plugins and WordPress have registered.

Matter of fact, we can't use any WordPress function that uses echo, and not return.

But don't fret. WordPress typically offers a solution to this caveat: get_the_title() vs the_title(), get_the_ID() vs the_ID(), and so on...

Reference: https://developer.wordpress.org/reference/functions/

Shared data

Location: /wp-content/themes/your-theme/functions.php

add_action('init', function () {
    // Synchronously using key/value
    Inertia::share('site_name', get_bloginfo('name'));

    // Synchronously using array
    Inertia::share([
        'primary_menu' => array_map(function ($menu_item) {
            return [
                'id'   => $menu_item->ID,
                'link' => $menu_item->url,
                'name' => $menu_item->title,
            ];
        }, wp_get_nav_menu_items('Primary Menu'))
    ]);

    // Lazily using key/callback
    Inertia::share('auth', function () {
        if (is_user_logged_in()) {
            return [
                'user' => wp_get_current_user()
            ];
        }
    });

    // Lazily on partial reloads
    Inertia::share('auth', Inertia::lazy(function () {
        if (is_user_logged_in()) {
            return [
                'user' => wp_get_current_user()
            ];
        }
    }));

    // Multiple values
    Inertia::share([
        // Synchronously
        'site' => [
            'name'       => get_bloginfo('name'),
            'description'=> get_bloginfo('description'),
        ],
        // Lazily
        'auth' => function () {
            if (is_user_logged_in()) {
                return [
                    'user' => wp_get_current_user()
                ];
            }
        }
    ]);
});

Asset Versioning

Location: /wp-content/themes/your-theme/functions.php

Optional, but helps with cache busting.

add_action('init', function () {
    // If you're using Laravel Mix, you can
    // use the mix-manifest.json for this.
    $version = md5_file(get_stylesheet_directory() . '/mix-manifest.json');

    Inertia::version($version);
});

boxybird/inertia-wordpress 适用场景与选型建议

boxybird/inertia-wordpress 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.22k 次下载、GitHub Stars 达 182, 最近一次更新时间为 2021 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「plugin」 「wordpress」 「inertia.js」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 boxybird/inertia-wordpress 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 boxybird/inertia-wordpress 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 182
  • Watchers: 8
  • Forks: 25
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-30