Hook: mam_contact_form_populate_contact

Signature

apply_filters( 'mam_contact_form_populate_contact', array $form );
Parameter Type Description
$form array The form structure being sent to the mobile app, including formid, fields, and per-field value entries.

Returns: array — the modified $form array. You must return it.


Purpose

Pre-fills custom fields on the contact form with values you compute server-side, before the form is delivered to the mobile app.

This is the companion to mam_gf_get_form_settings_fields_contact_form: that filter adds custom fields to the form, this filter populates them with values.


When it runs

Inside mam_contact_form_manager::populate_contact_form() (mam-contact-form.php), at the very end of the populate routine. The order of operations is:

  1. The form ID is checked against mam-contact-formcontact_form. If it doesn’t match, the filter still runs but the core populate has been skipped — useful if you’re a downstream plugin overriding behavior on a different form ID, but unusual.
  2. The current user is resolved via mam_user(). If there is no signed-in user, the filter still runs (with $form unmodified by core).
  3. Core populates the Name field with first_name + last_name, the Email field with user_email, and the Phone field with billing_phone user meta.
  4. mam_contact_form_populate_contact runs. ← your callback

This means your callback always sees the form after core has done its pre-fill on Name, Email, and Phone. You can override those values if you have a special case, but you typically don’t need to.


What’s already populated

Before this filter runs, mam-contact-form has already auto-populated the following defaults for signed-in users from their WordPress user profile:

Field slug Source
name first_name + last_name (also sets firstName and lastName keys on the field for split-name UI)
email user_email
phone billing_phone user meta — only if non-empty

You do not need to populate these yourself. Use this filter for custom fields you’ve added via mam_gf_get_form_settings_fields_contact_form, or to override the defaults if you have a special case (e.g., preferring a different name format for B2B users).


Example: pre-filling address fields

This example pre-fills the address fields added in Hook: mam_gf_get_form_settings_fields_contact_form with the user’s saved address from user meta:

add_filter( 'mam_contact_form_populate_contact', 'populate_contact_address' );

function populate_contact_address( $form ) {
    $user_id = get_current_user_id();
    if ( ! $user_id ) {
        return $form;
    }

    $address = [
        'street' => get_user_meta( $user_id, 'billing_address_1', true ),
        'city'   => get_user_meta( $user_id, 'billing_city',      true ),
        'state'  => get_user_meta( $user_id, 'billing_state',     true ),
        'zip'    => get_user_meta( $user_id, 'billing_postcode',  true ),
    ];

    foreach ( $form['fields'] as $index => $field ) {
        $slug = $field['slug'] ?? null;
        if ( $slug && isset( $address[ $slug ] ) && $address[ $slug ] !== '' ) {
            $form['fields'][ $index ]['value'] = $address[ $slug ];
        }
    }

    return $form;
}

Example: overriding the default name format

add_filter( 'mam_contact_form_populate_contact', 'use_company_name_for_b2b' );

function use_company_name_for_b2b( $form ) {
    $user_id = get_current_user_id();
    if ( ! $user_id ) {
        return $form;
    }

    $company = get_user_meta( $user_id, 'billing_company', true );
    if ( ! $company ) {
        return $form;
    }

    foreach ( $form['fields'] as $index => $field ) {
        if ( ( $field['slug'] ?? '' ) === 'name' ) {
            $form['fields'][ $index ]['value'] = $company;
        }
    }

    return $form;
}

When it fires

This filter runs each time the mobile app requests the contact form — typically when the user opens the contact screen. It does not run on submission.

If the user closes and reopens the form, the filter runs again. Values can be recomputed each time the form is shown (useful if the underlying data changes), but the callback should be fast for the same reason — it’s on the critical path for the mobile app’s response.


Gotchas

  • Always return $form. Forgetting return $form; will deliver an empty form to the mobile app. This is the most common mistake.
  • Do not modify field structure here. Use this filter for values only. Adding, removing, or renaming fields here will desync the form from its Gravity Forms definition. Use mam_gf_get_form_settings_fields_contact_form to add fields.
  • Match by slug, not by label or position. Field IDs and order can shift between releases; the slug is the stable contract.
  • Runs even when the form ID does not match. The two early-return branches in populate_contact_form() still call this filter before returning. Don’t assume the form passed to your callback is the contact form unless you check $form['formid'] yourself.
  • Runs on every form load. Keep external lookups out of this callback or cache aggressively. A 200ms callback turns into 200ms of latency on every contact form open.
  • Setting value to '' (empty string) doesn’t unset a core-populated value — it overwrites it with empty. If you want to defer to the user, omit the assignment instead.

Verification

This article was last verified against:

  • Plugin: mam-contact-form v2.1
  • Source: mam-contact-form/mam-contact-form.php (populate_contact_form())

Re-verify whenever the order of core populate steps changes, the field auto-population sources (first_name, user_email, billing_phone) change, or the filter is moved out of populate_contact_form().


  • Plugin: mam-contact-form
  • Recipe: Setting up a contact form (default path)
  • Recipe: Setting up a contact form with Gravity Forms
  • Hook: mam_gf_get_form_settings_fields_contact_form — add the fields before populating them
  • Hook: mam_contact_form_contact_processed

Metadata

Field Value
Article type Hook Reference
Plugin slug mam-contact-form
Applies to plugin version 2.1+
Category Extending MAM Suite
Hook type filter
Audience PHP developer
Applies to setup paths Default path AND Gravity Forms path
Last verified 2026-05-01
Contents

    Need Support?

    Can’t find the answer you’re looking for? Don’t worry we’re here to help!