grottopress/wordpress-meta-box
Composer 安装命令:
composer require grottopress/wordpress-meta-box
包简介
A utility to set up meta boxes in WordPress
关键字:
README 文档
README
A utility to set up meta boxes in WordPress
Installation
Install via composer:
composer require grottopress/wordpress-meta-box
Usage
Set up single meta box
Follow the example below to set up a meta box:
<?php declare (strict_types = 1); namespace Vendor\Package; use GrottoPress\WordPress\MetaBox; use WP_Post; class MyAwesomeMetaBox { public function setUp() { \add_action('add_meta_boxes', [$this, 'add'], 10, 2); \add_action('save_post', [$this, 'save']); \add_action('edit_attachment', [$this, 'save']); } /** * @action add_meta_boxes */ public function add(string $post_type, WP_Post $post) { if (!($box = $this->box($post))) { return; } $this->metaBox($box)->add(); } /** * @action save_post * @action edit_attachment */ public function save(int $post_id) { if (!($box = $this->box(\get_post($post_id)))) { return; } $this->metaBox($box)->save($post_id); } /** * Define your meta box with the `box` method */ private function box(WP_Post $post): array { // if (\is_post_type_hierarchical($post->post_type)) { // return []; // } return [ 'id' => 'my-meta-box-1', 'title' => \esc_html__('My Meta box'), 'context' => 'side', 'priority' => 'default', 'screen' => 'page', // 'callback' => function ($arg) {}, // If set, 'fields' is ignored // 'callbackArgs' => [], // Passed as `$arg` to 'callback' above. Required if 'callback' is set. // 'saveCallback' => function ($post_id, $arg) {}, // Save with this callback instead of default. Required if 'callback' is set. // 'saveCallbackArgs' => [], // Passed as `$arg` to 'saveCallback' above. 'fields' => [ // See https://github.com/grottopress/wordpress-field [ // Field 1 'id' => 'my-meta-box-field-1', 'type' => 'select', 'choices' => [ 'left' => \esc_html__('Left'), 'right' => \esc_html__('Right'), ], 'label' => \esc_html__('Select direction'), 'labelPos' => 'before_field', // or 'after_field' 'sanitizeCallback' => 'sanitize_text_field' ], [ // Field 2 'id' => 'my-meta-box-field-2', 'type' => 'text', // ... ] ], 'notes' => '<p>'.\esc_html__('Just a super cool meta box example').'</p>', ]; } private function metaBox(array $args): MetaBox { return new MetaBox($args); } } // Add your meta box to WordPress $myMetaBox = new MyAwesomeMetaBox(); $myMetaBox->setUp();
Set up multiple meta boxes
You may set up multiple meta boxes all at the same time, by returning an array of arrays (an array of meta box args) in the box() method in our example above:
// ... public function box(WP_Post $post) { return [ [ // Meta box 1 'id' => 'meta-box-1', 'context' => 'normal', 'fields' => [ [ // Field 1 ], [ // Field 2 ] ], // ... ], [ // Meta box 2 'id' => 'meta-box-2', 'context' => 'side', // ... ], // ... ]; }
Then, use a foreach loop for both add() and save() methods:
// ... public function add(string $post_type, WP_Post $post) { if (!($boxes = $this->box($post))) { return; } foreach ($boxes as $box) { $this->metaBox($box)->add(); } } // ... public function save(int $post_id) { if (!($boxes = $this->box(\get_post($post_id)))) { return; } foreach ($boxes as $box) { $this->metaBox($box)->save($post_id); } }
Development
Run tests with composer run test.
Contributing
- Fork it
- Switch to the
masterbranch:git checkout master - Create your feature branch:
git checkout -b my-new-feature - Make your changes, updating changelog and documentation as appropriate.
- Commit your changes:
git commit - Push to the branch:
git push origin my-new-feature - Submit a new Pull Request against the
GrottoPress:masterbranch.
grottopress/wordpress-meta-box 适用场景与选型建议
grottopress/wordpress-meta-box 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 926 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「meta-box」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 grottopress/wordpress-meta-box 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grottopress/wordpress-meta-box 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 grottopress/wordpress-meta-box 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easily create custom meta boxes in WordPress.
The most powerful & comprehensive plugin to create, manage, show and connect dynamic data with forms and custom fields effortlessly on WordPress.
Integrates Meta Box custom fields with FacetWP. Make custom fields filterable.
Put Yoast SEO meta boxes at the end of the page.
Connect and display custom fields created by the Meta Box plugin in the Elementor’s dynamic tags.
Add custom fields for comments in WordPress. Support 40+ field types with easy config.
统计信息
- 总下载量: 926
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-06