Skip to content
View as Markdown

Smart Codes

FluentCRM Core Intermediate

Smart Codes are merge tags (like {{contact.first_name}}) that get replaced with dynamic values when emails are sent. You can register custom SmartCode groups to expose your plugin's data in FluentCRM emails, automation fields, and templates.

Built-in SmartCode Groups

FluentCRM ships with these SmartCode groups:

GroupPrefixExamples
Contactcontact{{contact.first_name}}, {{contact.email}}, {{contact.phone}}
Custom Fieldscontact.custom{{contact.custom.company_size}}
Companycontact.company{{contact.company.name}}, {{contact.company.industry}}
CRM / Businesscrm{{crm.business_name}}, {{crm.business_address}}
WordPresswp{{wp.admin_email}}
Dynamic Contentother{{other.latest_post.title}}, {{other.date.+2 days}}
User Metauser.meta{{user.meta.billing_phone}}

Default Values

Provide a fallback value after a | separator:

{{contact.first_name|Friend}}

If first_name is empty, "Friend" is used instead.

Transformers

Apply a text transformation after ||:

{{contact.first_name||ucfirst}}
{{contact.last_name||strtoupper}}

Available transformers:

TransformerDescription
trimTrims whitespace
ucfirstCapitalizes the first letter
ucwordsCapitalizes the first letter of each word
strtolowerConverts to lowercase
strtoupperConverts to uppercase
concat_firstPrepends a string (e.g., {{contact.first_name||concat_first|Hi}} → "Hi John")

Combining Default Values and Transformers

{{contact.first_name|Friend||ucfirst}}

The syntax is: {{group.key|default_value||transformer}}

Registering Custom SmartCodes

Use the Extender API to add your own SmartCode group:

php
add_action('fluent_crm/after_init', function () {
    $key = 'my_plugin';
    $title = 'My Plugin Data';
    $shortCodes = [
        'membership_level' => 'Membership Level',
        'points_balance'   => 'Points Balance',
        'join_date'        => 'Join Date',
    ];
    $callback = function ($code, $valueKey, $defaultValue, $subscriber) {
        $userId = $subscriber->user_id;

        if (!$userId) {
            return $defaultValue;
        }

        switch ($valueKey) {
            case 'membership_level':
                return get_user_meta($userId, 'membership_level', true) ?: $defaultValue;
            case 'points_balance':
                return get_user_meta($userId, 'points_balance', true) ?: $defaultValue;
            case 'join_date':
                $date = get_user_meta($userId, 'join_date', true);
                return $date ? date('F j, Y', strtotime($date)) : $defaultValue;
            default:
                return $defaultValue;
        }
    };

    FluentCrmApi('extender')->addSmartCode($key, $title, $shortCodes, $callback);
});

This registers three SmartCodes that can be used in emails:

  • {{my_plugin.membership_level}} — outputs the contact's membership level
  • {{my_plugin.points_balance|0}} — outputs points balance, defaults to "0"
  • {{my_plugin.join_date}} — outputs the formatted join date

API Reference

FluentCrmApi('extender')->addSmartCode($key, $title, $shortCodes, $callback)

ParameterTypeDescription
$keyStringUnique group identifier. Use your plugin prefix to avoid conflicts. Cannot use reserved keys (see below).
$titleStringDisplay title shown in the SmartCode picker dropdown
$shortCodesArrayAssociative array of 'value_key' => 'Display Label' pairs
$callbackCallableFunction called when parsing each SmartCode in this group

Callback parameters:

ParameterTypeDescription
$codeStringThe full SmartCode string (e.g., {{my_plugin.membership_level}})
$valueKeyStringThe key portion after the dot (e.g., membership_level)
$defaultValueStringThe default value specified by the user (text after |)
$subscriberSubscriberThe contact being processed

Reserved keys (cannot be used as $key): crm, other, contact, wp, fluentcrm, user, learndash, tutorlms, aff_wp, edd_customer, lifterlms, woo_customer

Source: app/Api/Classes/Extender.php