Snippets: Add Custom Metaboxes in the Post Editor

The Total theme comes with it's own custom build meta framework that you can easily make use of in your child theme to create new Metaboxes for your site without the need of a plugin. This framework is used in the Cards metabox as well as the Font Manager. Here is an example showing how to create a new metabox.

Important: While this functionality is available we do usually recommend using a plugin like Advanced Custom Fields for adding your custom meta as it's easier to manage, but if you prefer doing things via your child theme go for it.

function my_register_custom_metaboxes() {
    if ( ! class_exists( 'WPEX_Meta_Factory' ) ) {
        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',
        'fields'   => array(
            array(
                'name' => esc_html__( 'Select example', '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' ),
                ),
                'desc' => esc_html__( 'Custom description if needed', 'text_domain' ),
            ),
            array(
                'name' => esc_html__( 'Text Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'text',
            ),
            array(
                'name' => esc_html__( 'Textarea Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'textarea',
            ),
            array(
                'name' => esc_html__( 'URL Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'url',
            ),
            array(
                'name' => esc_html__( 'Date Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'date',
            ),
            array(
                'name' => esc_html__( 'Upload Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'upload',
                'return' => 'id', // return ID or URL
            ),
            array(
                'name' => esc_html__( 'Icon Select', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'icon_select',
                'choices' => array(
                    '' => esc_html__( 'Empty Choice', 'text_domain' ),
                    'fa fa-home' => esc_html__( 'Home', 'text_domain' ),
                    'fa fa-bolt' => 'fa fa-bold', // note: you can add a name (see prev) or set the valu equal to the key. You can also use the wpex_ticons_list() function to grab a list of theme icons - see plugins/total-theme-core/inc/meta/class-wpex-card-meta.php for an example.
                ),
            ),
            array(
                'name' => esc_html__( 'Group/Repeater Type', 'text_domain' ),
                'id'   => 'YOUR_UNIQUE_CUSTOM_FIELD_NAME',
                'type' => 'group', // combine multiple settings into 1 that returns an array of the values.
                'fields' => array(
                    // Add the fields normally here like above
                ),
            ),
        )
    ) );
}
add_action( 'admin_init', 'my_register_custom_metaboxes' );

Version Required:5.0+

Snippet Type: PHP

All PHP snippets should be added via a child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)