If you are using FluentCRM, you may want to display custom fields from their FluentCRM contact on the frontend for logged in users.
I wrote a small shortcode that retrieves and displays custom field data from FluentCRM within WordPress. It’s designed to handle various data types and offer formatting options.
The snippet
/** * Shortcode to display FluentCRM custom fields. * * This function creates a shortcode that retrieves and displays custom field data * from FluentCRM within WordPress. It handles various data types and offers * formatting options for dates, lists, and more. * * @param array $atts Shortcode attributes. * @return string Formatted custom field value or empty string if not found. * * @author Taylor Drayson * @date 2023-08-20 */ function tct_fluentcrm_custom_fields( $atts ) { $defaults = array( 'field' => '', 'type' => '', 'format' => '', 'date_format' => get_option('date_format'), 'time_format' => get_option('time_format'), 'list_item_format' => '%s' // Default format for list items ); $atts = shortcode_atts($defaults, $atts, 'fluentcrm_custom_fields'); $field = $atts['field']; if (!$field) { return ''; } $contactApi = FluentCrmApi('contacts'); $contact = $contactApi->getCurrentContact(); if (!$contact) { return ''; } $custom_fields = $contact->custom_fields(); if (!isset($custom_fields[$field])) { return ''; } $value = $custom_fields[$field]; if(!$value){ return; } // Handle array values if (is_array($value)) { if ($atts['format'] === 'list') { $list_items = array_map(function($item) use ($atts) { return sprintf('<li>' . $atts['list_item_format'] . '</li>', $item); }, $value); return sprintf('<ul>%s</ul>', implode('', $list_items)); } else { return implode(', ', $value); } } // Handle date/datetime types if ($atts['type'] === 'date' || $atts['type'] === 'datetime') { $date_format = $atts['type'] === 'datetime' ? $atts['date_format'] . ' ' . $atts['time_format'] : $atts['date_format']; if ($atts['type'] === 'date') { $date = DateTime::createFromFormat('Y-m-d', $value); } else { $date = DateTime::createFromFormat('Y-m-d H:i:s', $value); } if ($date) { return date_i18n($date_format, $date->getTimestamp()); } } // Default: return the value as is return $value; } add_shortcode( 'fluentcrm_custom_fields', 'tct_fluentcrm_custom_fields' );
Copy
How it works
- The function sets up default attributes and merges them with user-provided ones.
- It retrieves the current FluentCRM contact.
- The specified custom field value is fetched for that contact.
- Depending on the field type and specified format, the value is processed and returned.
To implement this in your WordPress projects, you’ll need to add this code to your theme’s functions.php file or, preferably, a custom plugin. Once added, you can use the shortcode in posts, pages, or widgets.
Examples
- Basic usage:
[fluentcrm_custom_fields field="field_name"]
- Date field:
[fluentcrm_custom_fields field="birthday" type="date"]
- Datetime field:
[fluentcrm_custom_fields field="last_login" type="datetime"]
- Custom date format:
[fluentcrm_custom_fields field="birthday" type="date" date_format="F j, Y"]
- Checkbox field as a list:
[fluentcrm_custom_fields field="interests" format="list"]
- Formatted list items:
[fluentcrm_custom_fields field="interests" format="list" list_item_format="<strong>%s</strong>"]
Note that this shortcode will only display data for the currently logged-in user who is also a contact in your FluentCRM system. If there’s no current contact, it will return an empty string.