Aleš Sýkora / November 14, 2024 / 0 comments

Fluent Forms: ACF Taxonomy Field Mapping in User Registration form

Post summary: This guide explains how to map a taxonomy field from Fluent Forms registration form to an ACF Taxonomy field in the user profile.

Prerequisites

  • Fluent Forms Pro plugin
  • Advanced Custom Fields PRO plugin
  • Registration form in Fluent Forms
  • ACF Taxonomy field created for user profile
  • Custom taxonomy registered in WordPress

Implementation

1. Preparing ACF Field

  1. Create a new ACF field group for user profiles
  2. Add a Taxonomy field type
  3. Select your custom taxonomy
  4. Set the location rule to “User Form”
  5. Note down your ACF field name (e.g., ‘user_profession’)

2. Preparing Fluent Form

  1. Add a field for taxonomy selection (dropdown/multi-select)
  2. Configure it to show taxonomy terms
  3. Note down the field name (e.g., ‘profession’)
  4. Note your form ID from Fluent Forms dashboard

3. Code Implementation

<?php
/**
 * Great-tit.com ─ Mapping ACF Taxonomy field during user registration in Fluent Forms
 * @version 1.0
 */

add_action('wp_loaded', function() {
    if (!function_exists('get_field')) {
        return;
    }
    
    add_action('fluentform/submission_inserted', 'map_taxonomy_to_acf_field', 20, 3);
});

function map_taxonomy_to_acf_field($entryId, $formData, $form) {
    // Basic checks
    if (!function_exists('get_field') || !function_exists('update_field')) {
        return;
    }

    // Form ID check
    if ($form->id != YOUR_FORM_ID) {
        return;
    }

    try {
        // Get newly created user ID
        $submission = wpFluent()->table('fluentform_submissions')
            ->where('id', $entryId)
            ->first();
        
        if (!$submission || empty($submission->user_id)) {
            error_log('FF Registration Error: No user_id found in submission');
            return;
        }
        
        $user_id = $submission->user_id;
        
        // Verify user exists
        $user = get_user_by('ID', $user_id);
        if (!$user) {
            error_log('FF Registration Error: User not found with ID ' . $user_id);
            return;
        }

        // Mapping taxonomy field
        if (!empty($formData['profession'])) {
            update_field('user_profession', $formData['profession'], 'user_' . $user_id);
        }

    } catch (Exception $e) {
        error_log('FF Registration Critical Error: ' . $e->getMessage());
    }
}

Field Configuration

  1. ACF Taxonomy Field Options
    • Field Type: Taxonomy
    • Return Format: Term ID (recommended for mapping)
    • Selection Type: Select/Multi-select
    • Allow Null: Based on your requirements
  2. Fluent Forms Field
    • Use Select/Multi-select field type
    • Populate with taxonomy terms
    • Set appropriate validation rules

Example Usage

Single Taxonomy Term

// Simple profession taxonomy mapping
if (!empty($formData['profession'])) {
    update_field('user_profession', $formData['profession'], 'user_' . $user_id);
}

Multiple Taxonomy Terms

// Multiple skills taxonomy mapping
if (!empty($formData['skills']) && is_array($formData['skills'])) {
    update_field('user_skills', $formData['skills'], 'user_' . $user_id);
}

Important Notes

  1. Ensure taxonomy exists and is registered
  2. Check if terms exist before mapping
  3. Consider handling new term creation if needed
  4. Verify term IDs are being passed correctly

Troubleshooting

  1. Verify form ID matches your registration form
  2. Check field names in both Fluent Forms and ACF
  3. Ensure taxonomy terms exist
  4. Check PHP error log for detailed error messages

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts