Jon Kristian

Mixing Business with Pleasure since 1999

Custom WPForms Smart Tags with Arguments

The WPForms plugin is my new favorite form plugin. It’s very clean and super easy to use. However since it’s still relatively new in the game there are some things that you might have to do manually/the hard way.

For instance they have their own smart tags system to build up emails and notifications. Sorta like shortcodes but not that advanced. Right now if you wanted to create your own smart tags you can do that, but there isn’t any documented way to add parameters to them unless you want to dive in to the code … Until now.

Say you wanted your smart tag to trigger some specific event function upon form completion and that code required an argument?

{tracking_event="aSpecificEvent"}

You can use the following filter and preg_match to find the smart tag and value:

/**
 * Custom WPForms Smart Tag with argument.
 *
 * @link https://jonkristian.no/2018/02/custom-wpforms-smart-tags-arguments/
 */
function wpforms_custom_smart_tags( $content, $tag )
{
    // Check if there's a "tracking_event" tag.
    preg_match( "/\{tracking_event=\"(.+?)\"\}/", $content, $event );
    
    if( !empty( $event[1] ) )
    {
        // Fire our tracking event function.
        fire_tracking_event($event[1]);

        $value = ''; // We don't want to output anything.
        $content = str_replace( '{fbtrack_event="'.$event[1].'"}', $value, $content );
    }

    return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpforms_custom_smart_tags', 10, 2 );

Hopefully WPForms will develop better ways to do this in the future, but for now we can use filters.

I know you could probably override the editor in wpforms to allow shortcodes but for security measures I believe this is the better option.

«

© 2018 Jon Kristian. Theme by Anders Norén.