$npx -y skills add Lonsdale201/wp-agent-skills --skill fluentcrm-smartcodes-segmentsAdd and parse FluentCRM 3.x SmartCodes and build Pro dynamic contact segments. Covers FluentCrmApi('extender')->addSmartCode, parser syntax, fallback/default values, transformers, funnel context smart codes, dynamic segment filters, and ContactsQuery advanced filter providers. Us
| 1 | # FluentCRM: SmartCodes and dynamic segments |
| 2 | |
| 3 | Use this skill when a plugin needs to expose custom values inside FluentCRM emails, automation fields, templates, or Pro dynamic segments. Keep SmartCode parsing read-only and privacy-aware; these values can appear in outgoing emails. |
| 4 | |
| 5 | Verification note: SmartCode registration and parsing are core FluentCRM 3.1.8 behavior. Dynamic segments are Pro behavior verified against local FluentCampaign Pro 3.1.8. |
| 6 | |
| 7 | ## When to use this skill |
| 8 | |
| 9 | - Adding custom merge tags such as `{{my_plugin.plan_name}}`. |
| 10 | - Parsing personalized FluentCRM text outside the normal campaign send path. |
| 11 | - Adding context SmartCodes to automation editors for order, booking, LMS, or other event data. |
| 12 | - Registering a Pro dynamic segment or advanced contact filter provider. |
| 13 | - Reviewing code that hooks `fluent_crm/extended_smart_codes`, `fluent_crm/smartcode_group_callback_*`, `fluentcrm_dynamic_segments`, or `fluentcrm_contacts_filter_*`. |
| 14 | |
| 15 | ## Register SmartCodes |
| 16 | |
| 17 | Use the public Extender API after FluentCRM init: |
| 18 | |
| 19 | ```php |
| 20 | add_action('fluent_crm/after_init', function () { |
| 21 | if (!function_exists('FluentCrmApi')) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | FluentCrmApi('extender')->addSmartCode( |
| 26 | 'my_plugin', |
| 27 | 'My Plugin', |
| 28 | [ |
| 29 | 'plan_name' => 'Plan Name', |
| 30 | 'renewal_date' => 'Renewal Date', |
| 31 | ], |
| 32 | function ($code, $valueKey, $defaultValue, $subscriber) { |
| 33 | $userId = $subscriber ? (int) $subscriber->getWpUserId() : 0; |
| 34 | if (!$userId) { |
| 35 | return $defaultValue; |
| 36 | } |
| 37 | |
| 38 | if ($valueKey === 'plan_name') { |
| 39 | return get_user_meta($userId, 'my_plugin_plan', true) ?: $defaultValue; |
| 40 | } |
| 41 | |
| 42 | if ($valueKey === 'renewal_date') { |
| 43 | $date = get_user_meta($userId, 'my_plugin_renewal_date', true); |
| 44 | return $date ? date_i18n(get_option('date_format'), strtotime($date)) : $defaultValue; |
| 45 | } |
| 46 | |
| 47 | return $defaultValue; |
| 48 | } |
| 49 | ); |
| 50 | }); |
| 51 | ``` |
| 52 | |
| 53 | The API key is `extender` in `app/Api/config.php`. `Extender.php` has an older docblock mentioning `extend`; use the registered config key unless you have runtime-tested an alias. |
| 54 | |
| 55 | `addSmartCode()` adds UI metadata through `fluent_crm/extended_smart_codes` and parser behavior through `fluent_crm/smartcode_group_callback_{key}`. Its callback signature is: |
| 56 | |
| 57 | ```php |
| 58 | function ($code, $valueKey, $defaultValue, $subscriber) { |
| 59 | return $defaultValue; |
| 60 | } |
| 61 | ``` |
| 62 | |
| 63 | Do not use reserved group keys: `crm`, `other`, `contact`, `wp`, `fluentcrm`, `user`, `learndash`, `tutorlms`, `aff_wp`, `edd_customer`, `lifterlms`, `woo_customer`. |
| 64 | |
| 65 | ## Parser behavior |
| 66 | |
| 67 | FluentCRM parses both syntaxes: |
| 68 | |
| 69 | ```text |
| 70 | {{group.key}} |
| 71 | ##group.key## |
| 72 | ``` |
| 73 | |
| 74 | Defaults use one pipe: |
| 75 | |
| 76 | ```text |
| 77 | {{contact.first_name|Friend}} |
| 78 | ``` |
| 79 | |
| 80 | Transformers use double pipe: |
| 81 | |
| 82 | ```text |
| 83 | {{contact.first_name||ucfirst}} |
| 84 | {{contact.email||strtolower}} |
| 85 | ``` |
| 86 | |
| 87 | Supported local transformers in 3.1.8 include `trim`, `ucfirst`, `strtolower`, `strtoupper`, `ucwords`, `concat_first`, `concat_last`, and `show_if`. |
| 88 | |
| 89 | When rendering custom text yourself, use the same filter FluentCRM uses: |
| 90 | |
| 91 | ```php |
| 92 | $body = apply_filters('fluent_crm/parse_campaign_email_text', $body, $subscriber); |
| 93 | ``` |
| 94 | |
| 95 | or call the parser directly: |
| 96 | |
| 97 | ```php |
| 98 | use FluentCrm\App\Services\Libs\Parser\Parser; |
| 99 | |
| 100 | $body = Parser::parse($body, $subscriber); |
| 101 | ``` |
| 102 | |
| 103 | Do not manually replace CRM unsubscribe and manage-subscription URLs early. `ShortcodeParser` intentionally leaves `crm.unsubscribe_url`, `crm.manage_subscription_url`, `crm.unsubscribe_html`, and `crm.manage_subscription_html` for a later parsing pass in email/external-page flows. |
| 104 | |
| 105 | ## Funnel context SmartCodes |
| 106 | |
| 107 | Use `fluent_crm_funnel_context_smart_codes` when a SmartCode should appear only for specific automation trigger contexts. |
| 108 | |
| 109 | ```php |
| 110 | add_filter('fluent_crm_funnel_context_smart_codes', function ($codes, $triggerName, $funnel) { |
| 111 | if ($triggerName !== 'my_plugin_e |