Jump to:

1778 Posts in 581 Topics by 555 members

Form Questions

SilverStripe Forums » Form Questions » Creating a Wufoo Shortcode instead of using UserDefinedForms

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 236 Views
  • JonYYC
    Avatar
    Community Member
    9 Posts

    Creating a Wufoo Shortcode instead of using UserDefinedForms Link to this post

    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!

    236 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.