mukto90/mdc-meta-box

一个PHP类,用于为WordPress的文章、页面或自定义文章类型创建自定义元框。

dev-master 2023-07-06 09:41 UTC

This package is auto-updated.

Last update: 2024-09-06 12:10:04 UTC


README

一个PHP类,用于为WordPress的文章、页面或自定义文章类型创建自定义元框。

用法

如何使用

如何添加元框

选项1(使用Composer)

  • 将以下内容添加到您的 composer.json 文件中-
{
    "require": {
        "mukto90/mdc-meta-box": "dev-master"
    }
}
  • 运行 composer install 命令。
  • 使用 /vendor/autoload.php; 包含您的自动加载文件(如果尚未包含)。
  • 实例化类并传递您的参数,如下所示-
$first_meta_field = new MDC_Meta_Box( $args ); // see below for sample $args

选项2

  • 在您的主题或插件中包含 class.mdc-meta-box.php
  • 在您的主题的 functions.php 文件或插件中定义元框和字段,如下所示-
$second_meta_field = new MDC_Meta_Box( $args ); // see below for sample $args

选项3

  • 简单地下载zip文件,并将其作为独立插件使用。
  • 在您的 wp-content/plugins 目录下编辑 mdc-meta-box/plugin.php

如何获取自定义字段值

  • 使用 get_post_meta( $post_id, 'your_field_name', true ) 获取字段值。

基本示例

// include library file
// require dirname( __FILE__ ) . '/src/class.mdc-meta-box.php'; // un-comment this if needed

add_action( 'admin_init', 'my_meta_fields' );

function my_meta_fields() {
    $args = array(
        'meta_box_id'   =>  'your_unique_meta_box_id_here',
        'label'         =>  __( 'Meta Box Title Here' ),
        'post_type'     =>  array( 'post', 'page', 'cpt1', 'cpt2' ),
        'context'       =>  'context_of_meta_box', // side|normal|advanced
        'priority'      =>  'priority_of_meta_box', // high|low
        'hook_priority' =>  'priority_of_hook', // Default 10
        'fields'        =>  array(
            /* adds a field */
            array(
                'name'      =>  'sample_field_name',
                'label'     =>  __( 'Field Title' ),
                'type'      =>  'field_type_here', // define a field type from text, number, textarea, file, radio etc. see 'Full Working Example' for better understanding
            ),
            /* adds another field */
            array(
                'name'      =>  'another_sample_field_name',
                'label'     =>  __( 'Another Field Title' ),
                'type'      =>  'field_type_here',
            ),
        )
    );

    mdc_meta_box( $args );
}

完整示例

// include library file
// require dirname( __FILE__ ) . '/src/class.mdc-meta-box.php'; // un-comment this if needed

add_action( 'admin_init', 'my_meta_fields' );

function my_meta_fields() {
    $args = array(
        'meta_box_id'   =>  'sample_meta_id',
        'label'         =>  __( 'Sample Meta Box' ),
        'post_type'     =>  array( 'post', 'page' ),
        'context'       =>  'normal', // side|normal|advanced
        'priority'      =>  'high', // high|low
        'hook_priority'  =>  10,
        'fields'        =>  array(
            /**
             * PLEASE NOTE
             * desc, desc_nop, class, default, readonly, disabled, cols, rows, text_mode, teeny and media_buttons are optional.
             */
            array(
                'name'      =>  'sample_text',
                'label'     =>  __( 'Text Field' ),
                'type'      =>  'text',
                'desc'      =>  __( 'This is a text field.' ),
                'class'     =>  'mdc-meta-field',
                'default'   =>  'Hello World!',
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
                'desc_nop'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_number',
                'label'     =>  __( 'Number Field' ),
                'type'      =>  'number',
                'desc'      =>  __( 'This is a number field.' ),
                'class'     =>  'mdc-meta-field',
                'default'   =>  10,
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_email',
                'label'     =>  __( 'Email Field' ),
                'type'      =>  'email',
                'desc'      =>  __( 'This is an email field.' ),
                'class'     =>  'mdc-meta-field',
                'default'   =>  'john@doe.com',
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_url',
                'label'     =>  __( 'URL Field' ),
                'type'      =>  'url',
                'desc'      =>  __( 'This is a url field.' ),
                'class'     =>  'mdc-meta-field',
                'default'   =>  'http://johndoe.com',
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_password',
                'label'     =>  __( 'Password Field' ),
                'type'      =>  'password',
                'desc'      =>  __( 'This is a password field.' ),
                'class'     =>  'mdc-meta-field',
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_textarea',
                'label'     =>  __( 'Textarea Field' ),
                'type'      =>  'textarea',
                'desc'      =>  __( 'This is a textarea field.' ),
                'class'     =>  'mdc-meta-field',
                'columns'   =>  24,
                'rows'      =>  5,
                'default'   =>  'lorem ipsum dolor sit amet',
                'readonly'  =>  false, // true|false
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_radio',
                'label'     =>  __( 'Radio Field' ),
                'type'      =>  'radio',
                'desc'      =>  __( 'This is a radio field.' ),
                'class'     =>  'mdc-meta-field',
                'options'   => array(
                    'item_1'  => 'Item One',
                    'item_2'  => 'Item Two',
                    'item_3'  => 'Item Three',
                    ),
                'default'   =>  'item_2',
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_select',
                'label'     =>  __( 'Select Field' ),
                'type'      =>  'select',
                'desc'      =>  __( 'This is a select field.' ),
                'class'     =>  'mdc-meta-field',
                'options'   => array(
                    'option_1'  => 'Option One',
                    'option_2'  => 'Option Two',
                    'option_3'  => 'Option Three',
                    ),
                'default'   =>  'option_2',
                'disabled'  =>  false, // true|false
                'multiple'  =>  true, // true|false
            ),
            array(
                'name'      =>  'sample_checkbox',
                'label'     =>  __( 'Checkbox Field' ),
                'type'      =>  'checkbox',
                'desc'      =>  __( 'This is a checkbox field.' ),
                'class'     =>  'mdc-meta-field',
                'disabled'  =>  false, // true|false
            ),
            array(
                'name'      =>  'sample_color',
                'label'     =>  __( 'Color Field' ),
                'type'      =>  'color',
                'desc'      =>  __( 'This is a color field.' ),
                'class'     =>  'mdc-meta-field',
                'default'   =>  '#f00'
            ),
            array(
                'name'      =>  'sample_wysiwyg',
                'label'     =>  __( 'WYSIWYG Field' ),
                'type'      =>  'wysiwyg',
                'desc'      =>  __( 'This is a wysiwyg field.' ),
                'class'     =>  'mdc-meta-field',
                'width'     =>  '100%',
                'rows'      =>  5,
                'teeny'     =>  true,
                'text_mode'     =>  false, // true|false
                'media_buttons' =>  false, // true|false
                'default'       =>  'Hello World'
            ),
            array(
                'name'      =>  'sample_fise',
                'label'     =>  __( 'File Field' ),
                'type'      =>  'file',
                'upload_button'     =>  __( 'Choose File' ),
                'select_button'     =>  __( 'Select File' ),
                'desc'      =>  __( 'This is a file field.' ),
                'class'     =>  'mdc-meta-field',
                'disabled'  =>  false, // true|false
                'default'   =>  'http://example.com/sample/file.txt'
            ),
        )
    );

    mdc_meta_box( $args );
}

截图

MDC Meta Box

需求(最小值)

  • PHP 5.3.0
  • WordPress 3.0+

作者

Nazmul Ahsan

讨论

链接