wordpressvn/wp-meta-box 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wordpressvn/wp-meta-box

Composer 安装命令:

composer require wordpressvn/wp-meta-box

包简介

Handy package to make creating WordPress meta boxes a breeze.

README 文档

README

This package aims to make it easier to create meta boxes for WordPress plugins.

⚠️ Untill the first stable release, the API is subject to change. Use at your own risk.

Installation

composer require wordpressvn/wp-meta-box

Usage

Basic example

use WPVNTeam\WPMetaBox\WPMetaBox;

$meta_box = WPMetaBox::post('Post settings')
    ->set_post_type('post');

$meta_box->add_option('text', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'description' => __('Some additional description', 'textdomain')
]);

$meta_box->make();

// Or for taxonomies:
$meta_box = WPMetaBox::taxonomy('Taxonomy settings')
    ->set_taxonomies(['category']);

Available types

Text

$meta_box->add_option('text', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Date

$meta_box->add_option('date', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Number

$meta_box->add_option('number', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Textarea

$meta_box->add_option('textarea', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Checkbox

$meta_box->add_option('checkbox', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Choices (radio buttons)

$meta_box->add_option('checkbox', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'options' => [
        1 => 'option 1',
        2 => 'option 2'
    ]
]);

Color

$meta_box->add_option('color', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Select

$meta_box->add_option('select', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'options' => [
        1 => 'option 1',
        2 => 'option 2'
    ]
]);

You can allow multiple values too.

$meta_box->add_option('select', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'multiple' => true,
    'options' => [
        1 => 'option 1',
        2 => 'option 2'
    ]
]);

Select2

Select2 gives you a customizable select box with support for searching.

You can use select2 the same way you use the regular select.

$meta_box->add_option('select2', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'options' => [
        1 => 'option 1',
        2 => 'option 2'
    ]
]);

If you would like to search the options through ajax, you can do this by defining two callbacks (or function names). One for fetching and filtering the options and one for getting the value callback.

The below example is using select2 to select a page.

$meta_box->add_option('select2', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'ajax' => [
        'value' => function($pageId) {
            return get_the_title($pageId) ?? null;
        },
        'action' => function() {
            $results = array_reduce(get_posts(['post_type' => 'page', 's' => $_GET['q']]), function($item, $page) {
                $item[$page->ID] = $page->post_title;

                return $item;
            }, []);

            echo json_encode($results);

            die();
        }
    ]
]);

You may allow multiple values by settings the multiple value in config to true. If you want to use the ajax functionality here, be sure to define value callback here as well.

$meta_box->add_option('select2', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain'),
    'multiple' => true,
    'ajax' => [
        'value' => function($ids) {
            foreach($ids as $id) {
                $titles[$id] = get_the_title($id) ?? $id;
            }
            return $titles ?? [];
        },
        'action' => function() {
            $results = array_reduce(get_posts(['post_type' => 'page', 's' => $_GET['q']]), function($item, $page) {
                $item[$page->ID] = $page->post_title;

                return $item;
            }, []);

            echo json_encode($results);

            die();
        }
    ]
]);

You can pass anything you'd like to the select2 configuration using config, the exception being the ajax part of the configuration.

A list of options can be found here.

The Select2 that comes with the package is loaded from the Cloudflare CDN. You can overwrite this using the wmb_select2_assets filter hook.

Media

$meta_box->add_option('media', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Image

$meta_box->add_option('image', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

Code editor

$meta_box->add_option('code-editor', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

WP Editor

$meta_box->add_option('wp-editor', [
    'name' => 'name_of_option',
    'label' => __('Label of option', 'textdomain')
]);

You can provide a config array to customize the editor. For more information on this config check out the wp.editor documentation.

Repeater

Example of a gallery using the repeater option:

$meta_box->add_option('repeater', [
    'name' => 'gallery',
    'label' => __('Gallery', 'textdomain'),
])->add_repeater_option('image', [
    'name' => 'image',
    'label' => __('Image', 'textdomain'),
]);

Contributors

License

MIT. Please see the License File for more information.

wordpressvn/wp-meta-box 适用场景与选型建议

wordpressvn/wp-meta-box 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 58 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 wordpressvn/wp-meta-box 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2024-03-02