Adding custom tabs and fields to the contact screen

Custom tabs and fields can be added to the contact screen using the plugins API and the contact meta DB.

You can see and read how on our Github or using the same code below.

<?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();
<br>
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