1. Home
  2. Docs
  3. WordPress Studio Framework
  4. Framework Locations
  5. Metabox Options

Metabox Options

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

NameTypeDefaultDescription
titlestring Title of the metabox
post_typearray|stringpostProvide any number of post_types for a given metabox to appear.
data_typestringserializeDatabase save option type (serialize | unserialize)
contextstringadvancedThe context within the screen where the boxes should display. (normal | side | advanced)
prioritystringdefaultThe priority within the context where the boxes should show. (high | low | default)
exclude_post_typesarray Array of post_types to exclude
page_templatesarray|string Bind visibility of a metabox to any number of page templates
post_formatsarray|string Bind the visibility of a metabox to a given post format
show_restoreboolfalseFlag to display restore button of the metabox
enqueue_webfontbooltrueFlag to load web fonts of the framework
async_webfontboolfalseFlag to load google fonts with async method of the metabox
output_cssbooltrueFlag to load output css of the framework
themestringdarkThe theme of the framework (dark | light)
classstring Extra CSS classes (space separated to append to the metabox
defaultsarray 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 );
Was this article helpful to you? Yes No

How can we help?