Aleš Sýkora / November 14, 2024 / 0 comments
Fluent Forms: ACF Link Field Mapping in User Registration form
3 min read / ACF, Custom Code, Fluent Forms / Share on: Twitter, LinkedIn, Facebook
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
- Create a new ACF field group for user profiles
- Add a Link field type
- Set the location rule to “User Form”
- Note down your ACF field name (e.g., ‘user_portfolio’)
2. Preparing Fluent Form
- Add a URL input field to your registration form
- Note down the field name (e.g., ‘url_portfolio’)
- 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
- 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
- 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
- Verify form ID matches your registration form
- Check field names in both Fluent Forms and ACF
- Ensure URL is properly formatted
- 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. 🍻