Skip to content

Creating Metaboxes

Darko Gjorgjijoski edited this page Mar 3, 2021 · 1 revision

Metaboxes can be easily created using the IgniteKit\WP\OptionBuilder\Framework class. The library supports multiple option pages as well.

The class instance needs to be before all other hooks. Meaning that you shouldn't instance it and register the options in init, plugins_loaded or other actions.

use IgniteKit\WP\OptionBuilder\Framework;

$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' ),
		),
	),
);

$framework->register_metabox($metabox);
Clone this wiki locally