Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba

Creating a Wufoo Shortcode instead of using UserDefinedForms


Go to End
Reply


3 Posts   1311 Views

Avatar
JonYYC

Community Member, 10 Posts

20 September 2012 at 10:19am

Edited: 20/09/2012 10:21am

I've used userdefinedforms for a number of sites but have found it lacking in some instances. So, we have started using wufoo as a user defined forms engine and build a short code handler to inject the proper javsascript code into your page. Wufoo already gives you a shortcode for wordpress that you can embed, so we adapted it to work for Silverstripe (2.4).

How to code it up:

Step 1: Add the following code file, wufooForm.php to your site:

<?php
class WufooForm {
    public static function CreateHtmlAndJavascript(array $arguments)
    {
        if (empty($arguments['username'])) { return "ShortCode Error: Missing username parameter."; }
        if (empty($arguments['formhash'])) { return "ShortCode Error: Missing formhash parameter."; }

        // Set defaults
        $customise = array();
        $customise['username'] = $arguments['username'];
        $customise['formhash'] = $arguments['formhash'];
        $customise['height'] = 500;
        $customise['autoresize'] = 'true';
        $customise['header'] = "show";
        $customise['ssl'] = 'false';

        //overide the defaults with the arguments supplied
        $customise = array_merge($customise,$arguments);

        // Generate Html and Javascript to be returned to the code in plaec of the ShortCode
        $htmlOutput = "<div id=\"wufoo-[formhash]\">Fill out my <a href=\"http://[username].wufoo.com/forms/[formhash]\">online form</a>.</div>";

        $htmlOutput .= "<script type=\"text/javascript\">var [formhash]; (function (d, t) { var s = d.createElement(t), options = {";
        $htmlOutput .= "'userName': '[username]','formHash': '[formhash]','autoResize': [autoresize],'height': '[height]','async': true,'header': '[header]','ssl': [ssl]};";
        $htmlOutput .= "s.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + 'wufoo.com/scripts/embed/form.js';";
        $htmlOutput .= "s.onload = s.onreadystatechange = function () {";
        $htmlOutput .= "    var rs = this.readyState; if (rs) if (rs != 'complete') if (rs != 'loaded') return;";
        $htmlOutput .= "    try { [formhash] = new WufooForm(); [formhash].initialize(options); [formhash].display(); } catch (e) { }";
        $htmlOutput .= "};";
        $htmlOutput .= "var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);";
        $htmlOutput .= "})(document, 'script');";
        $htmlOutput .= "</script>";

        $htmlOutput = str_replace('[username]', $customise['username'], $htmlOutput);
        $htmlOutput = str_replace('[formhash]', $customise['formhash'], $htmlOutput);
        $htmlOutput = str_replace('[height]', $customise['height'], $htmlOutput);
        $htmlOutput = str_replace('[autoresize]', $customise['autoresize'], $htmlOutput);
        $htmlOutput = str_replace('[header]', $customise['header'], $htmlOutput);
        $htmlOutput = str_replace('[ssl]', $customise['ssl'], $htmlOutput);

        return $htmlOutput;
    }

}

Step 2: Add the short code handler to your Page class


    public static function WufooShortCodeHandler($arguments)
    {
        return WufooForm::CreateHtmlAndJavascript($arguments);
    }

Step 3: Register the shortcode in your mysite/_config.php file

 

ShortcodeParser::get()->register('wufoo',array('Page','WufooShortCodeHandler')); 

Step 4: Do a dev/build and flush

Step 5: Go to wufoo, grab the short code and put it into a content area. You should see your form when you preview your page.


[wufoo username="yourusername" formhash="z9z9z9" autoresize="true" height="500" header="show" ssl="true"]

And that's it.

Enjoy!

Avatar
spijker1056

Community Member, 8 Posts

27 June 2013 at 4:08pm

Do you know if this code will work with SS3 ?, would be very handy if it did.

Cheers

Avatar
JonYYC

Community Member, 10 Posts

28 June 2013 at 3:06am

The code in the WufooForm class shouldn't have to change, but I'm not sure about the short code registration. Those parameters might have changed a bit. I haven't implemented it in a SS3 site yet. If you try it out and it requires changes, please post them here.

Thanks.
Jonathan.