ignitekit/wp-option-builder

WordPress的简单易用选项和Metabox框架

1.0.4 2022-03-27 07:47 UTC

This package is auto-updated.

Last update: 2024-09-22 19:57:12 UTC


README

轻量级简单的方式创建插件和主题选项以及WordPress的Metabox。受OptionTree启发。

描述

WP Option Builder试图通过创建与其他插件相比易于使用的完全响应式选项面板和元框,来弥合WordPress开发者、设计师和最终用户之间的差距。OptionBuilder具有许多高级功能,包括位置合适的钩子和过滤器,以调整用户体验的各个方面。

贡献

要贡献或报告错误,请访问WP Option Builder仓库。

选项类型

这是WP Option Builder附带的所有可用选项类型的完整列表。

  • 背景
  • 边框
  • 盒子阴影
  • 分类复选框
  • 分类选择
  • 复选框
  • 颜色选择器
  • 颜色选择器不透明度
  • CSS
  • 自定义帖子类型复选框
  • 自定义帖子类型选择
  • 日期选择器
  • 日期时间选择器
  • 尺寸
  • 图库
  • Google 字体
  • JavaScript
  • 链接颜色
  • 列表项
  • 测量
  • 数字滑块
  • 开/关
  • 页面复选框
  • 页面选择
  • 帖子复选框
  • 帖子选择
  • 单选按钮
  • 单选按钮图片
  • 选择
  • 侧边栏选择
  • 滑块
  • 社交链接
  • 间距
  • 标签页
  • 标签复选框
  • 标签选择
  • 分类复选框
  • 分类选择
  • 文本
  • 文本区域
  • 简单文本区域
  • 文本块
  • 带标题的文本块
  • 排版
  • 上传

安装

composer require ignitekit/wp-option-builder

或者如下不使用Composer

require_once '/path/to/wp-option-builder/autoload.php';

如何使用

假设你已经加载了所需的库,实例化Framework类

use IgniteKit\WP\OptionBuilder\Framework;
$framework = new Framework();

然后你可以注册选项页面和元框,如下所示

选项页面

$settings = array(
	'id'    => 'custom_options',
	'pages' => array(
		array(
			'id'              => 'test_page',
			'parent_slug'     => 'themes.php',
			'page_title'      => __( 'Theme Options', 'your-text-domain' ),
			'menu_title'      => __( 'Theme Options', 'your-text-domain' ),
			'capability'      => 'edit_theme_options',
			'menu_slug'       => 'demo-theme-options',
			'icon_url'        => null,
			'position'        => null,
			'updated_message' => __( 'Options updated!', 'your-text-domain' ),
			'reset_message'   => __( 'Options reset!', 'your-text-domain' ),
			'button_text'     => __( 'Save changes', 'your-text-domain' ),
			'show_buttons'    => true,
			'screen_icon'     => 'options-general',
			'contextual_help' => array(
				'content' => array( array(
						'id'      => 'option_types_help',
						'title'   => __( 'Option Types', 'theme-text-domain' ),
						'content' => '<p>' . __( 'Help content goes here!', 'theme-text-domain' ) . '</p>',
					),
				),
				'sidebar' => '<p>' . __( 'Sidebar content goes here!', 'theme-text-domain' ) . '</p>',
			),
			'sections' => array( array(
				'id'    => 'option_types',
				'title' => __( 'Option Types', 'theme-text-domain' ),
			    ),
			),
			'settings'        => array(
				array(
					'id'           => 'demo_background',
					'label'        => __( 'Background', 'theme-text-domain' ),
					'desc'         => __( 'Some description goes here...', 'theme-text-domain' ),
					'std'          => '',
					'type'         => 'background',
					'section'      => 'option_types',
					'rows'         => '',
					'post_type'    => '',
					'taxonomy'     => '',
					'min_max_step' => '',
					'class'        => '',
					'condition'    => '',
					'operator'     => 'and',
				),
				array(
					'id'           => 'demo_border',
					'label'        => __( 'Border', 'theme-text-domain' ),
					'desc'         => __( 'Some description goes here...', 'theme-text-domain' ),
					'std'          => '',
					'type'         => 'border',
					'section'      => 'option_types',
					'rows'         => '',
					'post_type'    => '',
					'taxonomy'     => '',
					'min_max_step' => '',
					'class'        => '',
					'condition'    => '',
					'operator'     => 'and',
				),
				array(
					'id'           => 'demo_box_shadow',
					'label'        => __( 'Box Shadow', 'theme-text-domain' ),
					'desc'         => __( 'Some description goes here...', 'theme-text-domain' ),
					'std'          => '',
					'type'         => 'box-shadow',
					'section'      => 'option_types',
					'rows'         => '',
					'post_type'    => '',
					'taxonomy'     => '',
					'min_max_step' => '',
					'class'        => '',
					'condition'    => '',
					'operator'     => 'and',
				),
			)
		)
	)
);

$framework->register_settings( array( $settings ) ); // Note: $settings one group option pages, you can add multiple groups of pages.

检索选项

要检索上述设置的选项,找到设置密钥,在我们的例子中是custom_options

// Make settings instance
$settings = get_option('custom_options');

// To access demo_background setting
$demo_bg = isset($settings['demo_background']) ? $settings['demo_background'] : ''; // or simply $settings['demo_background'].

元框

$framework->register_metabox( array(
	'id'       => 'demo_meta_box',
	'title'    => __( 'Demo Meta Box', 'theme-text-domain' ),
	'desc'     => '',
	'pages'    => array( 'post' ),
	'context'  => 'normal',
	'priority' => 'high',
	'fields'   => array(
		array(
			'label' => __( 'Conditions', 'theme-text-domain' ),
			'id'    => 'demo_conditions',
			'type'  => 'tab',
		),
		array(
			'label' => __( 'Show Gallery', 'theme-text-domain' ),
			'id'    => 'demo_show_gallery',
			'type'  => 'on-off',
			'desc'  => sprintf( __( 'Shows the Gallery when set to %s.', 'theme-text-domain' ), '<code>on</code>' ),
			'std'   => 'off',
		),
		array(
			'label'     => '',
			'id'        => 'demo_textblock',
			'type'      => 'textblock',
			'desc'      => __( 'Congratulations, you created a gallery!', 'theme-text-domain' ),
			'operator'  => 'and',
			'condition' => 'demo_show_gallery:is(on),demo_gallery:not()',
		),
		array(
			'label'     => __( 'Gallery', 'theme-text-domain' ),
			'id'        => 'demo_gallery',
			'type'      => 'gallery',
			'desc'      => sprintf( __( 'This is a Gallery option type. It displays when %s.', 'theme-text-domain' ), '<code>demo_show_gallery:is(on)</code>' ),
			'condition' => 'demo_show_gallery:is(on)',
		),
		array(
			'label' => __( 'More Options', 'theme-text-domain' ),
			'id'    => 'demo_more_options',
			'type'  => 'tab',
		),
		array(
			'label' => __( 'Text', 'theme-text-domain' ),
			'id'    => 'demo_text',
			'type'  => 'text',
			'desc'  => __( 'This is a demo Text field.', 'theme-text-domain' ),
		),
		array(
			'label' => __( 'Textarea', 'theme-text-domain' ),
			'id'    => 'demo_textarea',
			'type'  => 'textarea',
			'desc'  => __( 'This is a demo Textarea field.', 'theme-text-domain' ),
		),
	),
) );

检索元框设置

要检索元框设置,使用如下get_post_meta()函数。

$value = get_post_meta($post_id, 'demo_text', true);

如果你在元框设置中使用'save_mode' => 'array',则所有设置将存储在一个元行中,作为序列化数组。如果想要更好的性能,这种方法很好。数据可以通过一个mysql查询而不是为每个字段分别访问来获取。

要使用这种类型的save_mode检索,如下调用get_post_meta()函数

$data = get_post_meta($post_id, 'demo_meta_box', true);
$value = $data['demo_text'];

更多详情

许可证

Copyright (C) 2021 Darko Gjorgjijoski (https://darkog.com)

This file is part of WP Option Builder

WP Option Builder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

WP Option Builder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with WP Option Builder. If not, see <https://gnu.ac.cn/licenses/>.