The WordPress Studio Framework (WPSF) can also be used to create custom metaboxes that can be assigned to different post types.
Metabox Options Framework Arguments
Name | Type | Default | Description |
---|---|---|---|
title | string | Title of the metabox | |
post_type | array|string | post | Provide any number of post_types for a given metabox to appear. |
data_type | string | serialize | Database save option type (serialize | unserialize) |
context | string | advanced | The context within the screen where the boxes should display. (normal | side | advanced) |
priority | string | default | The priority within the context where the boxes should show. (high | low | default) |
exclude_post_types | array | Array of post_types to exclude | |
page_templates | array|string | Bind visibility of a metabox to any number of page templates | |
post_formats | array|string | Bind the visibility of a metabox to a given post format | |
show_restore | bool | false | Flag to display restore button of the metabox |
enqueue_webfont | bool | true | Flag to load web fonts of the framework |
async_webfont | bool | false | Flag to load google fonts with async method of the metabox |
output_css | bool | true | Flag to load output css of the framework |
theme | string | dark | The theme of the framework (dark | light) |
class | string | Extra CSS classes (space separated to append to the metabox | |
defaults | array | Sets all default values from an external array (optional) |
Simple Metabox Options Example
// Check to see if WordPress Studio Framework is loaded
if ( class_exists( 'WPSF' ) ){
$prefix = 'custom_post_options';
// Create a metabox
WPSF::createMetabox( $prefix, array(
'title' => 'Custom Post Options',
'post_type' => 'post',
));
// Create a section
WPSF::createSection( $prefix, array(
'title' => 'Tab Title One',
'fields' => array(
array(
'id' => 'example-text',
'type' => 'text',
'title' => 'Example Text',
),
)
));
WPSF::createSection( $prefix, array(
'title' => 'Tab Title Two',
'fields' => array(
array(
'id' => 'example-textarea',
'type' => 'textarea',
'title' => 'Example Textarea',
),
)
));
}
Metabox Option Framework with Side Location
if ( class_exists( 'WPSF' ) ){
$prefix = 'metabox_options';
WPSF::createMetabox( $prefix, array(
'title' => 'My Custom Metabox',
'post_type' => 'post',
'context' => 'side',
));
WPSF::createSection( $prefix, array(
'title' => 'Tab Title One',
'fields' => array(
array(
'id' => 'example-text',
'type' => 'text',
'title' => 'Example Text',
),
)
));
WPSF::createSection( $prefix, array(
'title' => 'Tab Title Two',
'fields' => array(
array(
'id' => 'example-textarea',
'type' => 'textarea',
'title' => 'Example Textarea',
),
)
));
}
Metabox Options Framework with all Arguments
if ( class_exists( 'WPSF' )){
$prefix = 'metabox_options';
WPSF::createMetabox( $prefix, array(
'title' => '',
'post_type' => 'post',
'data_type' => 'serialize',
'context' => 'advanced',
'priority' => 'default',
'exclude_post_types' => array(),
'page_templates' => '',
'post_formats' => '',
'show_restore' => false,
'enqueue_webfont' => true,
'async_webfont' => false,
'output_css' => true,
'theme' => 'dark',
'class' => '',
));
WPSF::createSection( $prefix, array(
'title' => 'Tab Title One',
'fields' => array(
array(
'id' => 'example-text',
'type' => 'text',
'title' => 'Example Text',
),
)
));
}
Extraction of Values from Metabox
// data_type => serialize is default
// The 'custom_post_options' is the unique ID of the metabox (prefix)
$meta = get_post_meta( get_the_ID(), 'custom_post_options', true );
echo $meta['example-text'];
echo $meta['example-textarea'];
Extraction of Values from Metabox – Advanced
// data_type => unserialize is optional
// you should use the option id to directly declare the field config
echo get_post_meta( get_the_ID(), 'example-text', true );
echo get_post_meta( get_the_ID(), 'example-textarea', true );