Aleš Sýkora / November 14, 2024 / 0 comments
Fluent Forms: ACF Taxonomy 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 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
- Create a new ACF field group for user profiles
- Add a Taxonomy field type
- Select your custom taxonomy
- Set the location rule to “User Form”
- Note down your ACF field name (e.g., ‘user_profession’)
2. Preparing Fluent Form
- Add a field for taxonomy selection (dropdown/multi-select)
- Configure it to show taxonomy terms
- Note down the field name (e.g., ‘profession’)
- 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
- 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
- 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
- Ensure taxonomy exists and is registered
- Check if terms exist before mapping
- Consider handling new term creation if needed
- Verify term IDs are being passed correctly
Troubleshooting
- Verify form ID matches your registration form
- Check field names in both Fluent Forms and ACF
- Ensure taxonomy terms exist
- 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. 🍻