Advanced Forms Documentation

website·Indexed 30 days ago

https://advancedforms.github.io/

Documentation for Advanced Forms Pro, featuring a comprehensive library of PHP actions and filters (such as af/form/args and af/form/success_message), the advanced_form() function, and guides on creating forms, calculated fields, and integrations with Mailchimp and Zapier.

Tokens
16K
Snippets
97
Records
135
Agent score
75%

What's inside Advanced Forms

  1. Overview of Advanced Forms for WordPress

    Advanced Forms is a WordPress plugin that enables the creation of front-end forms by leveraging Advanced Custom Fields (ACF). It supports all ACF field types, including repeaters and flexible content fields, allowing developers to use the standard ACF field editing interface to build forms.
  2. Access submission data using the AF() singleton

    The AF() function returns a singleton object containing metadata about the current submission. This object is accessible from all actions and filters and provides access to form arguments, created post IDs, and registered user IDs.
    // Get the form arguments
    $args = AF()->submission['args'];
    
    // Get the ID for a recently created post
    $postId = AF()->submission['post'];
    
    // Get the ID for a recently registered user
    $userId = AF()->submission['user'];
  3. Integrate Advanced Forms Pro with Mailchimp

    Connect your forms to Mailchimp to automatically subscribe visitors to your mailing lists. This requires a Mailchimp API key.

    Step 1: Generate an API Key

    1. Log in to your account at mailchimp.com.
    2. Click your profile name (top right) $\rightarrow$ Account.
    3. Navigate to the 'Extras' tab $\rightarrow$ API Keys $\rightarrow$ Create a Key.
    4. Copy the generated key.

    Step 2: Global Plugin Configuration

    1. In the WordPress admin panel, go to Forms $\rightarrow$ Settings.
    2. Paste the API key into the 'Mailchimp API key' field and save.

    Step 3: Form-Specific Configuration

    1. Edit the specific form you wish to connect.
    2. Go to the Integrations tab and enable the Mailchimp integration.
    3. Configure the following settings:
      • List field: Select the Mailchimp list for subscriptions.
      • Email field: Select the form field containing the email address (an ACF email field is recommended).
      • First/Last Name: Optionally map these fields to complete subscriber profiles.
  4. Create a multi-page form

    Multi-page forms are created by inserting 'Page' fields into the form structure to divide the content. These fields allow for numbering and custom button text (though only settings from the first page field are applied globally). The system automatically provides 'previous' and 'next' navigation buttons and a progression indicator. Each page is validated for errors before the user is allowed to progress to the next page.
  5. Set up email notifications for form submissions

    Forms can be configured to send an unlimited number of email notifications upon submission. For each notification, you can define a recipient (either a dynamic field value or a custom email address), a subject, and the email body. Field values can be inserted into the subject and content via the 'Insert field' dropdown. Optionally, a custom 'From' address can be specified.
    1. Navigate to the form settings page.
    2. Under 'Notifications', click 'Add new email' and name the notification.
    3. Set the recipient (Field or Custom).
    4. Define the subject and content.
    5. (Optional) Set the 'From' address.
  6. Process form submissions using the af/form/submission hook

    To process form data after a submission, use the af/form/submission action hook. Use the helper function af_get_field( 'field_name' ) to retrieve processed field values. This function returns values in the same format as ACF's get_field.

    Note: The $fields parameter passed to the hook is deprecated; use af_get_field instead.

    <?php
    
    function handle_form_submission( $form, $fields, $args ) {
        // Retrieve the value of the field named 'email'
        $email = af_get_field( 'email' );
    }
    add_action( 'af/form/submission', 'handle_form_submission', 10, 3 );
  7. Add hidden input fields to Advanced Forms

    Use the af/form/hidden_fields action to inject hidden HTML input elements into a form. These fields are useful for passing metadata (e.g., the current post title) along with the form submission.

    Data from these hidden fields is accessible via the $_POST object in submission hooks, such as af/form/submission.

    Important: Hidden inputs added via this action are not ACF fields and will not be automatically saved to entries or posts; you must handle the data manually in your submission logic.

    <?php
    
    function hidden_field( $form, $args ) {
        // The title can later be retrieved using $_POST['post_title'].
        $title = get_the_title();
        echo sprintf( '<input type="hidden" name="post_title" value="%s">', $title );
    }
    // Apply to a specific form key
    add_action( 'af/form/hidden_fields/key=FORM_KEY', 'hidden_field', 10, 2 );
  8. Integrate Advanced Forms Pro with Slack

    Set up instant Slack notifications whenever a visitor submits a form using an Incoming Webhook.

    Step 1: Create a Slack Webhook

    1. Visit my.slack.com/services/new/incoming-webhook/.
    2. Select the target channel and click 'Add Incoming WebHooks integration'.
    3. Copy the provided webhook URL.

    Step 2: Configure the Form

    1. In the WordPress admin, edit your form.
    2. Go to the Integrations tab and enable the Slack integration.
    3. Paste the webhook URL.
    4. Enter a custom notification message and select the form fields to be included in the Slack message.
  9. Install and activate Advanced Forms Pro

    To set up Advanced Forms Pro, purchase the plugin from hookturn.io to receive a license key and download link. If the free version of the plugin is currently installed, it must be disabled before activating the Pro version. After installation, the license key must be entered in the plugin settings to enable updates and support.
  10. Add and configure a basic calculated field

    Calculated fields update in real-time as a form is filled and are used for previews or displaying calculated values before submission. To add one: 1. Create a new ACF field of type 'Calculated' in your field group. 2. Navigate to the form settings and open the 'Calculated' tab. 3. Use the WYSIWYG editor for that field to enter content and mix in field values. Note: Basic mathematical arithmetic is not supported directly in the admin interface and requires code.
  11. Implement custom form-level validation logic

    For validation that requires checking multiple fields or complex form-wide logic, use the af/form/validate action. This action is triggered before form submission. Use af_get_field() to retrieve submitted data and af_add_error() to attach an error message to a specific field (by name or key).
    <?php
    function validate_name() {
        // Retrieve submitted field values
        $first_name = af_get_field( 'first_name' );
        $last_name = af_get_field( 'last_name' );
    
        // Perform custom validation logic
        if ( 'Fabian Lindfors' != $first_name . ' ' . $last_name ) {
            // af_add_error( field_name_or_key, error_message )
            af_add_error( 'first_name', 'Full name must be Fabian Lindfors!' );
        }
    }
    
    // Hook into a specific form using its key: af/form/validate/key=FORM_KEY
    add_action( 'af/form/validate/key=FORM_KEY', 'validate_name' );