Learn how to show or hide content on your WordPress website based on whether your visitors belong to specific FluentCRM tags, lists, or have specific custom field values. This powerful shortcode lets you create personalised experiences for your subscribers by displaying targeted content based on multiple criteria.
What Does This Shortcode Do?
The [fluentcrm_condition]
shortcode checks if the current visitor meets specific conditions in your FluentCRM setup. You can show different content based on:
- Whether they belong to certain tags
- Whether they’re part of specific lists
- A combination of tags and lists
- Whether they should or shouldn’t have these tags/lists
- Whether their custom fields match specific values
This is particularly useful for:
- Showing exclusive content to specific member groups
- Creating personalised messaging
- Managing access to different content tiers
- Setting up conditional navigation elements
- Displaying content based on membership levels or subscription status
Basic Usage
Here’s the simplest way to use the shortcode:
[fluentcrm_condition tags="tag1"] This content only shows to users with "tag1" [/fluentcrm_condition]
Copy
Understanding the Parameters
The shortcode accepts five parameters:
- tags: A comma-separated list of tag names or IDs
- lists: A comma-separated list of list names or IDs
- custom_fields: Custom field conditions in format “field_key:value” or “field_key:value1|value2”
- relation: Choose between ‘all’ (must match all conditions) or ‘any’ (must match at least one)
- has: Set to ‘yes’ (must have conditions) or ‘no’ (must not have conditions)
Advanced Examples
Show Content to Users in Multiple Tags
[fluentcrm_condition tags="vip,premium" relation="all"] This appears only for users who have both the VIP and Premium tags [/fluentcrm_condition]
Copy
Exclude Users from Specific Lists
[fluentcrm_condition lists="free-tier" has="no"] This content is hidden from free-tier subscribers [/fluentcrm_condition]
Copy
Complex Conditions
[fluentcrm_condition tags="customer" lists="newsletter,updates" custom_fields="membership_level:premium" relation="all"] Users see this if they're a customer, subscribed to both newsletter and updates, and have premium membership [/fluentcrm_condition]
Copy
Custom Field Examples
Show content based on membership level:
[fluentcrm_condition custom_fields="membership_level:premium"] This content is only for premium members [/fluentcrm_condition]
Copy
Show content for multiple field values (using the pipe separator):
[fluentcrm_condition custom_fields="status:active|pending"] This shows for users with either active or pending status [/fluentcrm_condition]
Copy
Complex condition with multiple custom fields:
[fluentcrm_condition custom_fields="membership_level:premium,country:UK|US" relation="all"] For premium members in UK or US only [/fluentcrm_condition]
Copy
Code Snippet
Add the code below to functions.php
or code snippet plugin.
/** * Shortcode: fluentcrm_condition * * This shortcode checks if a user meets specific FluentCRM tag, list, or custom field conditions. * * Attributes: * - tags (string): Comma-separated list of tag IDs or names. * - lists (string): Comma-separated list of list IDs or names. * - custom_fields (string): Comma-separated list of custom field conditions in format "field_key:value" or "field_key:value1|value2". * - relation (string): 'all' (require all) or 'any' (require any) of the tags/lists/custom_fields. * - has (string): 'yes' (user must have tags/lists/custom_fields) or 'no' (user must not have tags/lists/custom_fields). * * Example: * [fluentcrm_condition tags="tag1,tag2" lists="list1" custom_fields="membership_level:premium,status:active|pending" relation="all" has="yes"] * Content for users with all specified conditions. * [/fluentcrm_condition] * * @param array $atts Shortcode attributes. * @param string $content Content wrapped by the shortcode. * * @return string Processed content if conditions are met, or an empty string otherwise. */ function fluentcrm_condition_shortcode($atts, $content = null) { // Early return if FluentCRM is not available if (!function_exists('FluentCrmApi')) { return ''; } // Extract shortcode attributes with defaults $atts = shortcode_atts([ 'tags' => '', // Comma-separated list of tag IDs or names 'lists' => '', // Comma-separated list of list IDs or names 'custom_fields' => '', // Comma-separated list of custom field conditions 'relation' => 'any', // 'all' or 'any' (for tags, lists, and custom fields) 'has' => 'yes', // 'yes' or 'no' (whether the user should have or not have the conditions) ], $atts); // Get the current FluentCRM contact $contactApi = FluentCrmApi('contacts'); $contact = $contactApi->getCurrentContact(true, true); // Early return if no contact is found if (!$contact) { return ''; } // Parse attributes $tags = array_filter(array_map('trim', explode(',', $atts['tags']))); $lists = array_filter(array_map('trim', explode(',', $atts['lists']))); $custom_fields = array_filter(array_map('trim', explode(',', $atts['custom_fields']))); $relation = strtolower($atts['relation']) === 'all' ? 'all' : 'any'; $has = strtolower($atts['has']) !== 'no'; $conditions_met = []; // Check tags if (!empty($tags)) { $tag_check = ($relation === 'all') ? $contact->hasAllTagId($tags) : $contact->hasAnyTagId($tags); // Invert check if 'has' is 'no' if (!$has) { $tag_check = !$tag_check; } $conditions_met['tags'] = $tag_check; } // Check lists if (!empty($lists)) { $list_check = ($relation === 'all') ? $contact->hasAllListId($lists) : $contact->hasAnyListId($lists); // Invert check if 'has' is 'no' if (!$has) { $list_check = !$list_check; } $conditions_met['lists'] = $list_check; } // Check custom fields if (!empty($custom_fields)) { $contact_custom_fields = $contact->custom_fields(); $custom_field_results = []; foreach ($custom_fields as $field_condition) { // Parse field condition (format: "field_key:value" or "field_key:value1|value2") $parts = explode(':', $field_condition, 2); if (count($parts) !== 2) { continue; // Skip invalid format } $field_key = trim($parts[0]); $expected_values = array_map('trim', explode('|', $parts[1])); // Get the actual field value $actual_value = isset($contact_custom_fields[$field_key]) ? $contact_custom_fields[$field_key] : ''; // Handle different custom field types $field_match = false; if (is_array($actual_value)) { // Handle multi_select and checkboxes (arrays) // Check if any of the actual values match any of the expected values foreach ($actual_value as $val) { if (in_array($val, $expected_values, true)) { $field_match = true; break; } } } else { // Handle text, multi_line, numeric, select, radio, date, date_and_time (strings) $field_match = in_array($actual_value, $expected_values, true); } $custom_field_results[] = $field_match; } // Determine if custom fields condition is met based on relation $custom_fields_check = ($relation === 'all') ? !in_array(false, $custom_field_results, true) // All must be true : in_array(true, $custom_field_results, true); // At least one must be true // Handle empty custom field results if (empty($custom_field_results)) { $custom_fields_check = false; } // Invert check if 'has' is 'no' if (!$has) { $custom_fields_check = !$custom_fields_check; } $conditions_met['custom_fields'] = $custom_fields_check; } // Evaluate overall condition based on relation if (empty($conditions_met)) { return ''; // No conditions specified } $overall_result = ($relation === 'all') ? !in_array(false, $conditions_met, true) // All conditions must be true : in_array(true, $conditions_met, true); // At least one condition must be true // Return the wrapped content if all conditions are met return $overall_result ? do_shortcode($content) : ''; } add_shortcode('fluentcrm_condition', 'fluentcrm_condition_shortcode');
Copy
Code Breakdown
Let’s examine how the shortcode works:
Initial Checks
if (!function_exists('FluentCrmApi')) { return ''; }
Copy
This ensures FluentCRM is installed and active.
Contact Verification
$contactApi = FluentCrmApi('contacts'); $contact = $contactApi->getCurrentContact();
Copy
Gets the current visitor’s FluentCRM contact information.
Condition Processing
$tag_check = ($relation === 'all') ? $contact->hasAllTagId($tags) : $contact->hasAnyTagId($tags);
Copy
Checks if the contact meets the tag conditions based on the relation type.
Custom Field Processing
$contact_custom_fields = $contact->custom_fields(); foreach ($custom_fields as $field_condition) { $parts = explode(':', $field_condition, 2); if (count($parts) !== 2) { continue; // Skip invalid format } $field_key = trim($parts[0]); $expected_values = array_map('trim', explode('|', $parts[1])); } This section parses custom field conditions. The format "field_key:value" allows you to check for specific values, whilst "field_key:value1|value2" lets you check for multiple possible values. ### Handling Different Field Types if (is_array($actual_value)) { // Handle multi_select and checkboxes (arrays) foreach ($actual_value as $val) { if (in_array($val, $expected_values, true)) { $field_match = true; break; } } } else { // Handle text, multi_line, numeric, select, radio, date, date_and_time (strings) $field_match = in_array($actual_value, $expected_values, true); }
Copy
The code intelligently handles different custom field types. For array fields (like multi-select or checkboxes), it checks if any of the stored values match the expected values. For string fields, it does a direct comparison.
Conclusion
This enhanced shortcode provides a powerful way to create conditional content based on FluentCRM data. By supporting custom fields alongside tags and lists, you can create sophisticated content personalisation strategies. The flexible format for custom field conditions allows for both simple exact matches and multiple value checks, making it suitable for a wide range of use cases.
Remember to test thoroughly after implementation, especially with your specific custom field configurations. The shortcode works with all FluentCRM custom field types including text, select, multi-select, checkboxes, and more.