grottopress / wordpress-meta-box
WordPress中设置元框的工具
v1.0.0
2023-05-31 17:53 UTC
Requires
- php: >=7.0
- grottopress/wordpress-field: ^1.0
Requires (Dev)
README
WordPress中设置元框的工具
安装
通过composer安装
composer require grottopress/wordpress-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();
设置多个元框
您可以在上述示例的box()方法中返回一个数组的数组(一个元框参数数组),以同时设置多个元框
// ... 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', // ... ], // ... ]; }
然后,使用循环对add()和save()方法进行遍历
// ... 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); } }
开发
使用composer run test运行测试。
贡献
- 进行Fork
- 切换到
master分支:git checkout master - 创建您的功能分支:
git checkout -b my-new-feature - 进行更改,根据需要更新更改日志和文档。
- 提交更改:
git commit - 推送到分支:
git push origin my-new-feature - 针对
GrottoPress:master分支提交新的Pull Request。