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

Fluent Forms: ACF Link Field Mapping in User Registration form

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

Prerequisites

  • Fluent Forms Pro plugin
  • Advanced Custom Fields PRO plugin
  • Registration form in Fluent Forms
  • ACF Link field created for user profile

Implementation

1. Preparing ACF Field

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

2. Preparing Fluent Form

  1. Add a URL input field to your registration form
  2. Note down the field name (e.g., ‘url_portfolio’)
  3. Note your form ID from Fluent Forms dashboard

3. Code Implementation

<?php
/**
 * Great-tit.com ─ Mapping ACF Link 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_link_to_acf_field', 20, 3);
});

function map_link_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 link field
        if (!empty($formData['url_portfolio'])) {
            $link_array = array(
                'url' => esc_url($formData['url_portfolio']),
                'title' => '', // can be customized
                'target' => '_blank'
            );
            update_field('user_portfolio', $link_array, 'user_' . $user_id);
        }

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

Field Configuration

  1. ACF Link Field Structure
    • The field saves data as an array with url, title, and target
    • Default target is set to ‘_blank’ for external links
    • Title can be left empty or populated from another form field
  2. Fluent Forms Field
    • Use a simple URL input field
    • Ensure proper URL validation in form settings

Example Usage

Portfolio URL Example

// Simple portfolio URL mapping
if (!empty($formData['url_portfolio'])) {
    $link_array = array(
        'url' => esc_url($formData['url_portfolio']),
        'title' => 'Portfolio',
        'target' => '_blank'
    );
    update_field('user_portfolio', $link_array, 'user_' . $user_id);
}

Website URL with Custom Title

// Website URL with separate title field
if (!empty($formData['website_url'])) {
    $link_array = array(
        'url' => esc_url($formData['website_url']),
        'title' => !empty($formData['website_title']) ? 
                  sanitize_text_field($formData['website_title']) : 
                  'My Website',
        'target' => '_blank'
    );
    update_field('user_website', $link_array, 'user_' . $user_id);
}

Troubleshooting

  1. Verify form ID matches your registration form
  2. Check field names in both Fluent Forms and ACF
  3. Ensure URL is properly formatted
  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