Add New Custom Fields (Meta boxes)
Important
Be sure to check out the complete article on Adding Custom Fields first!
The Total theme comes with a custom-built meta framework. You can easily use it in your child theme to create new metaboxes for your site without the need for a plugin. Below is an example showing how to create a new metabox.
/**
* Register custom fields.
*/
function my_register_custom_metaboxes() {
if ( ! class_exists( 'WPEX_Meta_Factory' ) || ! is_admin() ) {
return;
}
new WPEX_Meta_Factory( array(
'id' => 'YOUR_UNIQUE_METABOX_ID',
'title' => esc_html__( 'Metabox Title', 'text_domain' ),
'screen' => array( 'post', 'page', 'portfolio' ), //post types to add the metabox to
'context' => 'normal',
'priority' => 'default',
// @important since Total Theme Core v1.7.1 you can now pass a function that returns the fields to prevent
// the fields from being stored in memory and so they are only called when needed.
'fields' => array(
// Select Field
array(
'name' => esc_html__( 'Select example', 'text_domain' ),
'desc' => esc_html__( 'Custom description if needed', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'select',
'choices' => array(
'' => esc_html__( 'Choice 1', 'text_domain' ),
'option-2' => esc_html__( 'Choice 2', 'text_domain' ),
),
),
// Button Group Field
array(
'name' => esc_html__( 'Button Select example', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'button_group',
'choices' => array(
'' => esc_html__( 'Choice 1', 'text_domain' ),
'option-2' => esc_html__( 'Choice 2', 'text_domain' ),
),
),
// Checkbox Field
array(
'name' => esc_html__( 'Checkbox example', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'checkbox',
),
// Text Field
array(
'name' => esc_html__( 'Text Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'text',
),
// HTML Field
array(
'name' => esc_html__( 'Text Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'text',
'allow_html' => true,
),
// Textarea Field
array(
'name' => esc_html__( 'Textarea Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'textarea',
),
// URL Field
array(
'name' => esc_html__( 'URL Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'url',
),
// Date Field
array(
'name' => esc_html__( 'Date Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'date',
),
// Upload Field
array(
'name' => esc_html__( 'Upload Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'upload',
'return' => 'id', // this is the value that will be saved, id or url (we recommend always storing attachment ID's)
'media_type' => 'image', // image, pdf, video, audio - important to define this so it renders nicely
),
// Group Field
array(
'name' => esc_html__( 'Group/Repeater Type', 'text_domain' ),
'id' => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
'type' => 'group',
'group_sort' => true, // allow drag/drop sorting of the fields
'group_title' => esc_html__( 'The Title for each group item (optional)', 'text_domain' ),
'fields' => array(
// Add your repeater fields here
),
),
)
) );
}
add_action( 'admin_init', 'my_register_custom_metaboxes' );All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Related Snippets