Add Content to Formidable Pro Forms Using PHP

formidable pro tutorial imageThis post and example shows how to add a hidden field using PHP, but it’s really a flexible method to add any content to Formidable Pro forms. Now the folks at Formidable Pro made it very easy to add a hidden field from within the form builder, so that’s not really what this post is about. What if you have a scenario where you want to add content, in our case a standard HTML hidden field, programatically via PHP? This way the value of the field could be dynamic.

The setup

Recently I was working on a plugin/Addon for Formidable Pro that will allow A/B testing of different forms. When a form is displayed, my plugin writes a value to a WordPress table, then when the form is submitted it goes back an updates that same entry. This required me to keep track of the first entry so the plugin knows which entry to update if the form gets submitted. The way I chose to do this was to just write a hidden field to the form that contains the entry ID–as the form is written to the page.

If I knew at the time I was building the form that I would use it in an A/B test, I could just create the hidden field and update the value, right? But for my plugin, I didn’t want users to have to add anything to their form in order to use it in an A/B test. That means my code has to figure out the form is being A/B tested and add the hidden field and value automatically, which turned out to be very easy.

How to write to the form

For adding the hidden field, I used the hook frm_entry_form. At the time I’m writing this, there is no documentation about this hook so you might not realize it exists. It’s pretty simple to use, though, so here we go…

That’s it for adding the field! Pretty simple, right?

If you’re not actually adding a dynamic value, but just some static content, you don’t need anything more than the echo statement. Then again, if we only wanted a static value then we have other methods to add it that don’t require programming.

This action is not a filter, but a hook. This means I’m not filtering the form’s code as it’s output, but hooking my content to the end. This is why there’s no return statement.

The plugin I mentioned above is now available for free in the WordPress Plugin Repository asĀ Formidable A/B Tests. I’ll write another article soon about how to make good use of A/B tests in your forms, but check it out in the meantime and let me know how you think I should improve it.

Leave a Comment