Skip to content

Snippet: Add New Custom Fields (Meta boxes)

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

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.

/**
 * Register custom fields.
 *
 * @link https://totalwptheme.com/docs/snippets/add-custom-metaboxes-to-total/
 */
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',
        // @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(
            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__( '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' );
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)
Back To Top