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.

2 Comments

  1. Is there a way to create a “Insert Custom Field” ” Smart Tag in WPForms

    Example: Insert Custom Field Smart Tag

    {Insert_Custom_Field=””}
    {Insert_Custom_Field=”ContactEmail”}
    {Insert_Custom_Field=”ContactName”}

    – where ContactEmail and ContactName are custom fields for current post

    Having this Smart Tag for instance would allow me to create a single WPForm and place it on a contact page template. I could then use {Insert_Custom_Field=”ContactEmail”} as the email address in the WPForms > Admin Notification > Send to Email Address. This way when a customer finds a contact that he or she is interested in they could fill out the form and the form would route to the contact.

    Giving the option to specify the custom field name for the post would give tremendous flexibility

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2018 Jon Kristian

Theme by Anders NorenUp ↑