ButterBean is a WordPress custom field management plugin that can help us better organize a lot of custom fields for the way the tabs in a MetaBox, when a certain post type of custom fields are especially many, this feature is particularly useful to help us make the custom field input interface is organized in a more logical way, the operation is more convenient. PopularWordPress eCommerce PluginWooCommerce is a similar implementation of the product field management interface.ButterBean can be achieved as shown below.
Install the ButterBean plugin
If we are not using the ButterBean in the standard plugin way, we need to copy the butterbean
folder into the theme directory and then load the butterbean.php
file, as follows.
add_action( 'plugins_loaded', 'th_load' );
function th_load() {
require_once( 'path/to/butterbean/butterbean.php' ); }
}
Registering ButterBean Callbacks
With the ButterBean plugin installed, we can use the butterbean_register
The action hooks register custom managers, sections, controls, and settings now, and the callback functions are $butterbean
and current $post_type
Two objects.
add_action( 'butterbean_register', 'th_register', 10, 2 );
function th_register( $butterbean, $post_type ) {
// 如果不是我们需要的文章类型,不执行
if ( 'your_post_type' !== $post_type )
return;
// 在这里注册 managers, sections, controls, 和 settings
}
Create manager manager
In ButterBean, Manager is a combination of Sections and Controls, a Manager is a Meta Box, we can register more than one Manager at the same time, the code is as follows:
$butterbean->register_manager(
'example',
array(
'label' => esc_html__( 'Example data', 'your-textdomain' ),
'context' => 'normal', 'priority' => 'high'
'priority' => 'high'
)
).
$manager = $butterbean->get_manager( 'example' );
Creating Custom Sections Sections
A Sections is a combination of Controls, displayed as a tab, and we can add custom labels, descriptions and icons to each tab.
$manager->register_section(
'section_1',
array(
'label' => esc_html__( 'dataset1', 'your-textdomain' ),
'icon' => 'dashicons-admin-generic'
)
).
Creating Custom Controllers Controls
A Controls is a custom field entry form item, the controller appears in a tab and is used to enter custom field data, Controls has a number of form types, from simple text boxes to WordPress media entry boxes, the types are comprehensive enough.
If we don't get what we need from the default Controls type, we can also extend the ButterBean_Control class to create custom Controls types.
$manager->register_control(
'abc_xyz', // same name as Settings below
array(
'type' => 'text', 'section' => 'section_1', // the same name as the Settings below
'section' => 'section_1', 'label' => eschtml
'label' => esc_html__( 'text1', 'your-textdomain' ), 'label' => esc_html__( 'text1', 'your-textdomain' ), 'attr' => array(
'attr' => array( 'class' => 'widefat' )
)
).
Creating customized settings
Settings defines how the data is saved. By default, Settings names are custom fields Key, which we can use on the front end with the standard WordPress get_post_meta
function to get the saved value, before saving the data, don't forget to use the sanitize_callback
function validates and purifies the data.
$manager->register_setting(
'abc_xyz', // same name as Controls above
array(
'sanitize_callback' => 'wp_filter_nohtml_kses'
)
).
If you are developing a more complex WordPress Themeor plug-ins, there are many custom fields need to manage, you can try to use the ButterBean plug-in to optimize the custom field input interface, I believe the user experience will have a relatively big improvement.