How to Display Conditional Content Based on FluentCRM Tags and Lists

tdrayson

Learn how to show or hide content on your WordPress website based on whether your visitors belong to specific FluentCRM tags or lists. This powerful shortcode lets you create personalised experiences for your subscribers by displaying targeted content.

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

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

Basic Usage

Here’s the simplest way to use the shortcode:

HTML

        [fluentcrm_condition tags="tag1"]
    This content only shows to users with "tag1"
[/fluentcrm_condition]
      
Copy

Understanding the Parameters

The shortcode accepts four parameters:

  1. tags: A comma-separated list of tag names or IDs
  2. lists: A comma-separated list of list names or IDs
  3. relation: Choose between ‘all’ (must match all conditions) or ‘any’ (must match at least one)
  4. has: Set to ‘yes’ (must have tags/lists) or ‘no’ (must not have tags/lists)

Advanced Examples

Show Content to Users in Multiple Tags

HTML

        [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

HTML

        [fluentcrm_condition lists="free-tier" has="no"]
    This content is hidden from free-tier subscribers
[/fluentcrm_condition]
      
Copy

Complex Conditions

HTML

        [fluentcrm_condition tags="customer" lists="newsletter,updates" relation="all"]
    Users see this if they're a customer AND subscribed to both newsletter and updates
[/fluentcrm_condition]
      
Copy

Code Snippet

Add the code below to functions.php or code snippet plugin.

PHP

        /**
 * Shortcode: fluentcrm_condition
 *
 * This shortcode checks if a user meets specific FluentCRM tag or list conditions.
 *
 * Attributes:
 * - tags (string): Comma-separated list of tag IDs or names.
 * - lists (string): Comma-separated list of list IDs or names.
 * - relation (string): 'all' (require all) or 'any' (require any) of the tags/lists.
 * - has (string): 'yes' (user must have tags/lists) or 'no' (user must not have tags/lists).
 *
 * Example:
 * [fluentcrm_condition tags="tag1,tag2" lists="list1" relation="all" has="yes"]
 *   Content for users with all specified tags and lists.
 * [/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
        'relation' => 'any',   // 'all' or 'any' (for tags and lists)
        'has' => 'yes',        // 'yes' or 'no' (whether the user should have or not have the tags/lists)
    ], $atts);

    // Get the current FluentCRM contact
    $contactApi = FluentCrmApi('contacts');
    $contact = $contactApi->getCurrentContact();

    // 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'])));
    $relation = strtolower($atts['relation']) === 'all' ? 'all' : 'any';
    $has = strtolower($atts['has']) !== 'no';

    // 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;
        }

        // Early return if tags condition fails
        if (!$tag_check) {
            return '';
        }
    }

    // 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;
        }

        // Early return if lists condition fails
        if (!$list_check) {
            return '';
        }
    }

    // Return the wrapped content if all conditions are met
    return do_shortcode($content);
}

add_shortcode('fluentcrm_condition', 'fluentcrm_condition_shortcode');

      
Copy

Code Breakdown

Let’s examine how the shortcode works:

Initial Checks

PHP

        if (!function_exists('FluentCrmApi')) {
    return '';
}
      
Copy

This ensures FluentCRM is installed and active.

Contact Verification

PHP

        $contactApi = FluentCrmApi('contacts');
$contact = $contactApi->getCurrentContact();
      
Copy

Gets the current visitor’s FluentCRM contact information.

Condition Processing

PHP

        $tag_check = ($relation === 'all') 
    ? $contact->hasAllTagId($tags) 
    : $contact->hasAnyTagId($tags);
      
Copy

Checks if the contact meets the tag conditions based on the relation type.

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise