WordPress Yet Easy Settings 是一个PHP类,用于轻松构建WordPress的高级管理页面。

1.0.8 2020-01-05 12:16 UTC

README

WordPress Yet Easy Settings 是一个PHP类,用于轻松构建WordPress的高级管理页面。

内置设置字段类型

  • 文本
  • URL
  • 电子邮件
  • 密码
  • 数字
  • 小数
  • 多行文本框
  • 复选框
  • 多选框
  • 下拉菜单
  • 多选下拉菜单
  • 单选按钮
  • 颜色选择器
  • 文件上传
  • 所见即所得

主要功能

  • 添加尽可能多的管理页面,可以将其放置在任何位置,无论是顶级管理菜单还是子菜单。
  • 添加自定义回调以渲染自定义设置字段类型。
  • 添加自定义回调以渲染自定义标签内容。
  • 添加自定义回调以渲染自定义页面内容。
  • 内置数据清洗和验证。
  • 轻松为管理页面添加帮助标签。
  • 轻松为管理页面添加自定义操作按钮。
  • 排入自定义脚本和样式。
  • 内置数据验证。

如何使用

安装

composer require sofyansitorus/wp-yes

在包含WP_Yes类文件之后,您只需要初始化WP_Yes类,然后按照顺序添加设置对象属性、添加标签、添加部分和字段。

简单的管理页面设置

这是初始化设置页面而不定义标签和部分的简单方法。

if ( ! function_exists( 'wp_yes_simple' ) ) {
    function wp_yes_simple() {

        $settings = new WP_Yes( 'wp_yes_simple' ); // Initialize the WP_Yes class.

        $settings->add_field(
            array(
                'id' => 'field_1',
                'label' => 'Field 1',
                'required' => true,
                'type'     => 'text',
            )
        );

        $settings->add_field(
            array(
                'id' => 'field_2',
                'label' => 'Field 2',
                'required' => false,
                'type'     => 'email',
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}

add_action( 'init', 'wp_yes_simple' );

我们还可以通过使用WP_Yes::add_fields方法批量添加设置字段。

if ( ! function_exists( 'wp_yes_simple_bulk' ) ) {
    function wp_yes_simple_bulk() {

        $settings = new WP_Yes( 'wp_yes_simple_bulk' ); // Initialize the WP_Yes class.

        $settings->add_fields(
            array(
                array(
                    'id' => 'field_1',
                    'label' => 'Field 1',
                    'required' => true,
                    'type'     => 'text',
                ),
                array(
                    'id' => 'field_2',
                    'label' => 'Field 2',
                    'required' => true,
                    'type'     => 'email',
                )
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}

add_action( 'init', 'wp_yes_simple_bulk' );

带部分的简单管理页面设置

这是初始化设置页面而不定义标签和部分的简单方法。

if ( ! function_exists( 'wp_yes_simple_with_section' ) ) {
    function wp_yes_simple_with_section() {

        $settings = new WP_Yes( 'wp_yes_simple_with_section' ); // Initialize the WP_Yes class.

        $settings->add_section(
            array(
                'id' => 'section_1',
                'title' => 'Section 1',
            )
        );

        $settings->add_fields(
            array(
                array(
                    'id' => 'field_1',
                    'label' => 'Field 1',
                    'required' => true,
                    'type'     => 'text',
                ),
                array(
                    'id' => 'field_2',
                    'label' => 'Field 2',
                    'required' => true,
                    'type'     => 'email',
                )
            )
        );

        $settings->add_section(
            array(
                'id' => 'section_2',
                'title' => 'Section 2',
            )
        );

        $settings->add_fields(
            array(
                array(
                    'id' => 'field_3',
                    'label' => 'Field 3',
                    'required' => true,
                    'type'     => 'text',
                ),
                array(
                    'id' => 'field_4',
                    'label' => 'Field 4',
                    'required' => true,
                    'type'     => 'email',
                )
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}

add_action( 'init', 'wp_yes_simple_with_section' );

多标签管理页面设置

默认情况下,设置页面只有一个标签。如果您想添加更多标签,只需在每个标签的最后一个WP_Yes::add_field之后调用WP_Yes::add_tab方法,然后依次调用WP_Yes::add_sectionWP_Yes::add_field方法。

if ( ! function_exists( 'wp_yes_multi_tabs' ) ) {
    function wp_yes_multi_tabs() {
        $settings = new WP_Yes( 'wp_yes_multi_tabs' ); // Initialize the WP_Yes class.

        $settings->add_tab(
            array(
                'id' => 'tab_1',
                'title' => 'Tab 1',
            )
        );

        $settings->add_section(
            array(
                'id' => 'section_1',
                'title' => 'Section 1',
            )
        );

        $settings->add_field(
            array(
                'id'       => 'field_1',
                'required' => true,
                'type'     => 'text',
            )
        );

        $settings->add_section(
            array(
                'id' => 'section_2',
                'title' => 'Section 2',
            )
        );

        $settings->add_field(
            array(
                'id'       => 'field_2',
                'label' => 'Field 2',
                'required' => true,
                'type'     => 'text',
            )
        );

        $settings->add_tab( // <-- Add tab 2.
            array(
                'id' => 'tab_2',
                'title' => 'Tab 2',
            )
        );

        $settings->add_fields(
            array(
                array(
                    'id' => 'field_3',
                    'label' => 'Field 3',
                    'required' => true,
                    'type'     => 'text',
                ),
                array(
                    'id' => 'field_4',
                    'label' => 'Field 4',
                    'required' => true,
                    'type'     => 'email',
                )
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}
add_action( 'init', 'wp_yes_multi_tabs' );

请注意,您需要在WP_Yes构造函数中传递的menu_slug参数和WP_Yes::add_field方法参数中的字段id键具有唯一值。您可以在不同的页面菜单中具有相同的标签id,也可以在不同的标签中具有相同的部分id。

带自定义操作按钮和帮助标签的管理页面设置

要向管理页面添加帮助标签和自定义操作按钮,您需要在调用WP_Yes::init方法之前,在任何地方调用WP_Yes::add_help_tabWP_Yes::add_button方法。

if ( ! function_exists( 'wp_yes_button_and_help_tabs' ) ) {
    function wp_yes_button_and_help_tabs() {

        $settings = new WP_Yes( 'wp_yes_button_and_help_tabs' ); // Initialize the WP_Yes class.

        $settings->add_help_tab(  // <-- Add help tab 1.
            array(
                'id'      => 'my_help_tab',
                'title'   => __( 'My Help Tab' ),
                'content' => '<p>' . __('Descriptive content that will show in My Help Tab-body goes here.') . '</p>',
            )
        );

        $settings->add_help_tab(  // <-- Add help tab 2.
            array(
                'id'      => 'my_help_tab2',
                'title'   => __( 'My Help Tab2' ),
                'content' => '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here 2. ') . '</p>',
            )
        );

        $settings->add_field(
            array(
                'id' => 'field_1',
                'label' => 'Field 1',
            )
        );

        $settings->add_button( 'Custom Action Button', 'index.php' ); // <-- Add custom action button.

        $settings->init(); // Run the WP_Yes class.
    }
}
add_action( 'init', 'wp_yes_button_and_help_tabs' );

获取存储的选项值

通过调用内置WordPress get_option函数并使用字段id作为第一个参数来获取选项值。

get_option( 'field_1' );

如果您在WP_Yes构造函数中将$setting_prefix值设置为第三个参数,或者使用WP_Yes::set_prefix方法设置前缀,那么在调用get_option函数时需要预先添加该前缀。

if ( ! function_exists( 'wp_yes_with_prefix' ) ) {
    function wp_yes_with_prefix() {

        $settings = new WP_Yes( 'wp_yes_with_prefix', array(), 'my_setting_prefix' ); // Initialize the WP_Yes class.

        $settings->add_field(
            array(
                'id' => 'field_1',
                'label' => 'Field 1',
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}
add_action( 'init', 'wp_yes_with_prefix' );

// To get stored option value for setting field field_1
get_option( 'my_setting_prefix_field_1' );
if ( ! function_exists( 'wp_yes_with_set_prefix' ) ) {
    function wp_yes_with_set_prefix() {

        $settings = new WP_Yes( 'wp_yes_with_set_prefix', array() ); // Initialize the WP_Yes class.

        $settings->set_prefix( 'my_setting_prefix2' );

        $settings->add_field(
            array(
                'id' => 'field_1',
                'label' => 'Field 1',
            )
        );

        $settings->init(); // Run the WP_Yes class.
    }
}
add_action( 'init', 'wp_yes_with_set_prefix' );

// To get stored option value for setting field field_1
get_option( 'my_setting_prefix2_field_1' );

有关添加自定义标签内容、添加自定义页面内容等更高级示例,请参阅示例文件夹中可用的所有示例。

屏幕截图

简单的设置表单

Simple Setting Form

所有字段类型

All Fields Types All Fields Types

带有标签的设置表单

Setting Form with Tabs

带有操作按钮的管理页面

Admin Page with Action Button

带有帮助标签的管理页面

Admin Page with Help Tabs

自定义标签内容

Custom Tab Content

自定义页面内容

Custom Page Content

子菜单管理页面

Sub-menu Admin Page