Creating Custom SmartCode for Form Editor

Fluent Forms have custom smartcodes for dynamic data that you can use for form input’s default value. By default you will get wide range of dynamic smart-codes including current user info, current embedded post’s information, dynamic dates, cookie value, URL get parameters, Browser information, IP address etc.

But If you want to add your own smartcodes then it’s not that hard actually. Let’s see how you can add your own custom smartcode.

Pushing Smartcode to the suggested list #

add_filter('fluentform_editor_shortcodes', function ($smartCodes) {
    $smartCodes[0]['shortcodes']['{my_own_smartcode}'] = 'My Own smartcode';
    return $smartCodes;
});

By using this code we have hooked the fluentform_editor_shortcodes filter hook and then added our own smartcode {my_own_smartcode} in the available list.

Smart code pushed in Available suggestion

Transforming the value #

Now our smartcode has been available and anyone can use that in any input field. Let’s transform that value with something dynamic.

/*
 * To replace dynamic new smartcode the filter hook will be
 * fluentform_editor_shortcode_callback_{your_smart_code_name}
 */
add_filter('fluentform_editor_shortcode_callback_my_own_smartcode', function ($value, $form) {
    $dynamicValue = $form->title; // We are send current form title. You can fetch any data and return
    return $dynamicValue;
}, 10, 2);

So in this example, We are adding a composite filter fluentform_editor_shortcode_callback_my_own_smartcode. Here my_own_smartcode is the keyword of our SmartCode and then returning the current form’s title. You can definitely fetch your own value and return. Please note that You have to always return number or string. No array or object return is allowed.

Dynamic Default Value showing now

Build Geo Location SmartCode #

Let’s build something useful. Some hosting provides provide $_SEREVR variable for geo-data based on IP address. Please note that It depends on your hosting provider. So please check if these $_SEREVR variables are available or not.

  • $_SERVER[‘GEOIP_COUNTRY_NAME’] – Return Country Name
  • $_SERVER[‘GEOIP_CITY’] – Return City name

So let’s create a smartcode {ffc_geo_country} smartcode which will return the user’s country name.

/*
 * Add the smartcode to the list
 */
add_filter('fluentform_editor_shortcodes', function ($smartCodes) {
    $smartCodes[0]['shortcodes']['{ffc_geo_country}'] = 'GEO Country';
    return $smartCodes;
});

/*
 * Transform the smartcode
 */
add_filter('fluentform_editor_shortcode_callback_ffc_geo_country', function ($value, $form) {
    if(isset($_SERVER['GEOIP_COUNTRY_NAME'])) {
        return $_SERVER['GEOIP_COUNTRY_NAME'];
    }
    return '';
}, 10, 2);

That’s it. It’s too easy to extend Fluent Forms. Please check the project at github.

If you did not start using Fluent Forms then you should definitely give a try. It’s Free and available in WordPress Plugin repository

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *