Adding custom tabs and fields to the contact screen

it’s very easy to add custom tabs and fields to the contact screen using the plugins API and the contact meta DB.

See it on GitHub >> https://github.com/tobeyadr/groundhogg-code-examples/blob/0d482f60cccd19e199e32d032afe917a8e860aa7/2.0/custom-tab.php

<?php


class My_Custom_Tab
{

    /**
     * My_Custom_Tab constructor.
     */
    function __construct()
    {
        add_filter( 'groundhogg/admin/contact/record/tabs', [ $this, 'register_tab' ] );
        add_action( 'groundhogg/admin/contact/record/tab/my_custom_tab', [ $this, 'render' ] );
        add_action( 'groundhogg/admin/contact/save', [ $this, 'save' ], 10, 2 );
    }


    /**
     *
     * Register you tab using a unique ID and a name.
     *
     * @param $tabs
     * @return mixed
     */
    public function register_tab( $tabs )
    {
        $tabs[ 'my_custom_tab' ] = __( 'My Custom Tab' );


        return $tabs;
    }


    /**
     * Render the output of the tab
     *
     * @param $contact \Groundhogg\Contact
     */
    public function render( $contact )
    {
        ?>
        <h3>My Custom Tab</h3>
        <table class="form-table">
            <tbody>
            <tr>
                <th>custom Field</th>
                <td><input type="text" name="my_custom_field" value="<?php esc_attr_e( $contact->get_meta( 'my_custom_field' ) ); ?>"></td>
            </tr>
            </tbody>
        </table>
        <?php
    }


    /**
     * Save the custom fields.
     *
     * @param $contact_id int
     * @param $contact \Groundhogg\Contact
     */
    public function save( $contact_id, $contact )
    {

        $my_custom_field_value = sanitize_text_field( \Groundhogg\get_request_var( 'my_custom_field', 'My Default value' ) );

        if ( $my_custom_field_value ){
            $contact->update_meta( 'my_custom_field', $my_custom_field_value );
        }

    }
    
}


$my_new_tab = new My_Custom_Tab();

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us