after quite a bit of tedious script writing I've been able to get some results with integrating a formtools api based form with wordpress shortcodes using snippets. I'll post my code here for anyone who needs a reference.
A couple of notes before I show you the code
First off this requires you include the API in the index.php file for wordpress. This is similar to the API documentation. Its quite simple but here's a piece of code as an example:
Next, here's an example of the shortcode that I'm using. The attribute 'mode' for the [ft_snippet_contact_form] shortcode has three values: 'test','initialize' and 'live'. < <= = >= > have all been replaced by lt lte e gte and gt as they tend to mess with formatting in the wordpress post/page editor.
There's quite a few form elements missing as I've only tailored this script for one form.
And here's the php snippet. The php snippet code documentation should explain the shortcode attributes:
I'm using a wordpress plugin called code snippets to store this php snippet.
Hope this helps anyone who's looking to develop this further...
EDIT: ohh and here's an example of the thankyou page shortcode
A couple of notes before I show you the code
First off this requires you include the API in the index.php file for wordpress. This is similar to the API documentation. Its quite simple but here's a piece of code as an example:
PHP Code:
require_once($_SERVER["DOCUMENT_ROOT"]."/{formtools_folder}/global/api/api.php");
/** Loads the WordPress Environment and Template */
require('./{wordpress_folder}/wp-blog-header.php');
Next, here's an example of the shortcode that I'm using. The attribute 'mode' for the [ft_snippet_contact_form] shortcode has three values: 'test','initialize' and 'live'. < <= = >= > have all been replaced by lt lte e gte and gt as they tend to mess with formatting in the wordpress post/page editor.
Code:
[ft_snippet_contact_form next_page="./thankyou/" form_data="post" mode="live" form_number="2"]
<fieldset>
[ft_snippet_input name="contact_name" title="Name: " size=85 formtools_rules="required,Please enter your name.|length lte 100,Name must be less than 100 characters in length."]
[ft_snippet_input name="email2" title=" Email: " size=85 formtools_rules="required,Please enter your email address.|valid_email,Please enter a valid email address."]
[ft_snippet_bademail name="email" title="Re-enter email: " size=85]
</fieldset>
<fieldset>
[ft_snippet_input name="subject" title="Subject: " size=85 get=true formtools_rules="required,Please select a subject.|length lte 100,Subject must be less than 100 characters in length"]
[ft_snippet_textarea name name="message" title="Message: " cols=50 rows=8 note="Maximum length is 2000 characters" formtools_rules="required, Please enter your message"]
</fieldset>
[ft_snippet_reset name="reset"][ft_snippet_submit name="submit" value="Submit"]
[/ft_snippet_contact_form]
And here's the php snippet. The php snippet code documentation should explain the shortcode attributes:
PHP Code:
// -------------------------------------------------------------------------
// Validation functions for shortcode attributes
// -------------------------------------------------------------------------
//-Formtools Snippet Name Attribute Validator
// Description: checks the name attribute for shortcode. Its designed to be used
// on form tags
// Returns: String describing error if conflict exists, false otherwise
//
function ft_snippets_validate_name( $name ) {
$wp_reserved_names = array( "name" => true, "day" => true, "month" => true, "year" => true);
if( array_key_exists( $name, $wp_reserved_names ) && ( $wp_reserved_names[$name] == true ) ) :
return "Error: The shortcode name attribute '".$name."' is used by wordpress for post data. Reserved wordpress names values '".implode("', '", array_keys( $wp_reserved_names ))."'";
else :
return false;
endif;
}
// -------------------------------------------------------------------------
// Nested Shortcode Functions for [ft_snippet_contact_form]
// -------------------------------------------------------------------------
//-Formtools Snippet Bademail Shortcode
// Shortcode: [ft_snippet_bademail name="email" title="Re-enter email: " size=85]
// Returns: Formatted invisible email field used to check for simple spambots
//
function ft_snippets_shortcode_badinput( $atts ) {
extract( shortcode_atts( array(
'name' => 'email',
'title' => 'Re-enter email: ',
'size' => 85
), $atts ) );
$error = ft_snippets_validate_name($name);
if ( $error !== false ) :
return $error;
else :
//might need to add to a global rather than return here if there is recursion problem
return '<label style="display: none;">'.$title.'<input type="text" name="'.$name.'" size="'.$size.'" value="" /></label>';
endif;
}
add_shortcode('ft_snippet_bademail', 'ft_snippets_shortcode_badinput');
//-Formtools Snippet Input Shortcode
// Shortcode: [ft_snippet_input name="name" title="Name: " size=85 get=false formtools_rules="" format=false]
// Populates: $ft_snippet_data_array["rules"] array with formatted formtools validation rules
// Returns when format=false: Same shortcode minus formtools_rules and format=true (loads formtools_rules into global array)
// Returns when format=true: Formatted input field
//
function ft_snippets_shortcode_input( $atts ) {
//grab the global data array for the formtools snippet
global $ft_snippet_data_array;
extract( shortcode_atts( array(
'name' => 'contact_name',
'title' => 'Name: ',
'size' => 85,
'get' => false,
'formtools_rules' => '',
'format' => false
), $atts ) );
$error = ft_snippets_validate_name($name);
if ( $error !== false ) :
return $error;
else :
//if on the first run we just want to extract the rules for validation and return a snippet to format later.
if (($format == "false") && ($ft_snippet_data_array["context"] == "nested")) :
if (!empty($formtools_rules)) :
$formtools_rules = explode("|", $rules);
foreach ($formtools_rules as $rule) :
$search = array(",", " lt ", " lte", " e ", " gte ", " gt ");
$replace = array(",".$name.",", "<", "<=", "=", ">=", ">");
$ft_snippet_data_array["rules"][] = str_replace($search, $replace, $rule);
endforeach;
endif;
//return the shortcode with the formtools_rules stripped and format=true
return '[ft_snippet_input name="'.$name.'" title="'.$title.'" size='.$size.' get='.$get.' format=true]';
//On the second run we want to use the updated values after validation and format the snippet in html.
else :
if($get && isset($_GET[$name]) && ($ft_snippet_data_array["form_data"] == $_POST)) :
$value = $_GET[$name];
$readonly = true;
else :
$value = $ft_snippet_data_array["fields"][$name];
$readonly = false;
endif;
//return the formatted input field
return '<label>'.$title.'<input name="'.$name.'" type="text" size="'.$size.'" value="'.$value.'"'.(($readonly) ? ' READONLY' : '').' /></label>';
endif;
endif;
}
add_shortcode('ft_snippet_input', 'ft_snippets_shortcode_input');
//-Formtools Snippet Textarea Shortcode
// Shortcode: [ft_snippet_textarea name="name" title="Name: " cols=50 rows=8 note="Maximum length is 2000 characters" formtools_rules="" format=false]
// Populates: $ft_snippet_data_array["rules"] array with formatted formtools validation rules
// Returns when format=false: Same shortcode minus formtools_rules and format=true (loads formtools_rules into global array)
// Returns when format=true: Formatted textarea
//
function ft_snippets_shortcode_textarea( $atts ) {
//grab the global data array for the formtools snippet
global $ft_snippet_data_array;
extract( shortcode_atts( array(
'name' => 'message',
'title' => 'Message: ',
'cols' => 50,
'rows' => 8,
'note' => 'Maximum length is 2000 characters.',
'formtools_rules' => '',
'format' => false
), $atts ) );
$error = ft_snippets_validate_name($name);
if ( $error !== false ) :
return $error;
else :
//if on the first run we just want to extract the rules for validation and return a snippet to format later.
if (($format == "false") && ($ft_snippet_data_array["context"] == "nested")) :
if (!empty($formtools_rules)) :
$formtools_rules = explode("|", $rules);
foreach ($formtools_rules as $rule) :
$search = array(",", " lt ", " lte", " e ", " gte ", " gt ");
$replace = array(",".$name.",", "<", "<=", "=", ">=", ">");
$ft_snippet_data_array["rules"][] = str_replace($search, $replace, $rule);
endforeach;
endif;
//return the shortcode with the formtools_rules stripped and format=true
return '[ft_snippet_textarea name="'.$name.'" title="'.$title.'" cols='.$cols.' rows='.$rows.' note="'.$note.'" format=true]';
//On the second run we want to use the updated values after validation and format the snippet in html.
else :
$value = $ft_snippet_data_array["fields"][$name];
//return the formatted textarea
return "<label>\n ".$title."<textarea name='".$name."' cols='".$cols."' rows='".$rows."'>".$value."</"."textarea>\n <span class='note'>".$note."</span>\n";
endif;
endif;
}
add_shortcode('ft_snippet_textarea', 'ft_snippets_shortcode_textarea');
//-Formtools Snippet Reset Button Shortcode
// Shortcode: [ft_snippet_reset name="reset"]
// Returns: Formatted reset input tag
//
function ft_snippets_shortcode_reset( $atts ) {
extract( shortcode_atts( array(
'name' => 'reset'
), $atts ) );
$error = ft_snippets_validate_name($name);
if ( $error !== false ) :
return $error;
else :
return '<input name="'.$name.'" type="reset" />';
endif;
}
add_shortcode('ft_snippet_reset', 'ft_snippets_shortcode_reset');
//-Formtools Snippet Submit Button Shortcode
// Shortcode: [ft_snippet_submit name="submit" value="Submit"]
// Populates: $ft_snippet_data_array["submit"] with the name of the submit button
// Returns: Formatted submit input tag
//
function ft_snippets_shortcode_submit( $atts ) {
//grab the global data array for the formtools snippet
global $ft_snippet_data_array;
extract( shortcode_atts( array(
'name' => 'submit_button',
'value' => 'Submit'
), $atts ) );
$error = ft_snippets_validate_name($name);
if ( $error !== false ) :
return $error;
else :
//store the name of the submit button in the data array and return formatted submit input tag
$ft_snippet_data_array["submit"] = $name;
return '<input name="'.$name.'" type="submit" value="'.$value.'" />';
endif;
}
add_shortcode('ft_snippet_submit', 'ft_snippets_shortcode_submit');
// -------------------------------------------------------------------------
// Main shortcode function for [ft_snippet_contact form]
// -------------------------------------------------------------------------
//-Formtools Snippet Contact Form Shortcode
// Shortcode: [ft_snippet_contact_form next_page="thanks.php" form_data="post" mode="test" form_number=1]content including other formtools shortcodes[/ft_snippet_contact_form]
// Returns if no submission: Formatted HTML form
// Returns if submission: Redirect to next page
//
function ft_snippets_shortcode_contact_form( $atts, $content ) {
global $wpdb;
//first off, lets re-establish the connection to the
//formtools database that was broken by the wordpress
//connection..
$g_link = ft_db_connect();
//create a global data array for the formtools shortcode so
//other nested shortcodes can access and modify the data
global $ft_snippet_data_array;
$ft_snippet_data_array = array();
extract( shortcode_atts( array(
'next_page' => './thankyou/',
'form_data' => 'post',
'mode' => 'test',
'form_number' => 1
), $atts ) );
//set the form data type used to submit data in the form
if ($form_data == "post") :
$ft_snippet_data_array["form_data"] = $_POST;
else :
$ft_snippet_data_array["form_data"] = $_GET;
endif;
$form_number = intval($form_number);
//set the default value for the submit button, initialise the rules array and set the context to nested
$ft_snippet_data_array["submit"] = "submit_button";
$ft_snippet_data_array["rules"] = array();
$ft_snippet_data_array["context"] = "nested";
//start the formtools initialisation based on mode
switch($mode) :
case "live" : $ft_snippet_data_array["fields"] = ft_api_init_form_page($form_number); break;
case "initialize" : $ft_snippet_data_array["fields"] = ft_api_init_form_page($form_number,"initialize"); break;
default : $ft_snippet_data_array["fields"] = ft_api_init_form_page("","test"); break;
endswitch;
//run the first instance of do_shortcode() to format shortcodes without rules
//and grab rules from & return updated shortcodes from shortcodes with rules
$content = do_shortcode($content);
//now we have all the relevant data to start the form validation
if ( isset( $ft_snippet_data_array["form_data"][$ft_snippet_data_array["submit"]] ) ) :
//validate the most recently submitted form data only if form is live
if ($mode == "live") :
$errors = validate_fields( $ft_snippet_data_array["form_data"], $ft_snippet_data_array["rules"] );
endif;
//process the submission if no errors returned
if ( empty( $errors ) ) :
$params = array(
"submit_button" => $ft_snippet_data_array["submit"],
"next_page" => $next_page,
"form_data" => $ft_snippet_data_array["form_data"],
"finalize" => true
);
//process the submission and redirect to next page
ft_api_process_form($params);
//if errors update the fields with the most recently submitted form data
else :
$ft_snippet_data_array["fields"] = array_merge($_SESSION["form_tools_form"], $ft_snippet_data_array["form_data"]);
endif;
endif;
//after field values have been updated, run the second instance of do_shortcode()
//to format shortcode returned by first instance of do_shortcode()
$content = do_shortcode($content);
$returned_content = "<form action='".htmlentities($_SERVER["REQUEST_URI"])."' method='post' name='contact'>\n";
if (!empty($errors)) :
$returned_content .= "\t<div class='error'><ul>\n";
foreach ($errors as $error)
$returned_content .= "\t\t<li><div>".$error."</div></li>\n";
$returned_content .= "\t</ul></div>\n";
endif;
$returned_content .= $content;
$returned_content .= "</form>";
//re-establish connection with wordpress database so we don't
//break anything delicate...
$wpdb->db_connect();
return $returned_content;
}
add_shortcode('ft_snippet_contact_form', 'ft_snippets_shortcode_contact_form');
// -------------------------------------------------------------------------
// Main shortcode function for [ft_snippet_thanks]
// -------------------------------------------------------------------------
//-Formtools Snippet Thanks Shortcode
// Shortcode: [ft_snippet_contact_form next_page="thanks.php" form_data="post" mode="test" form_number=1]content including other formtools shortcodes[/ft_snippet_contact_form]
// Returns if no submission: Formatted HTML form
// Returns if submission: Redirect to next page
//
function ft_snippets_shortcode_thanks( $attr , $content) {
//clear the session and post data
$fields = ft_api_init_form_page();
ft_api_clear_form_sessions();
extract( shortcode_atts( array(
'title' => 'Thanks for your enquiry!'
), $atts ) );
//do_shortcode() on the content incase any encapsulated shortcode is contained within
$content = do_shortcode($content);
//return formatted content
$returned_content = "<h2>".$title."</h2>\n".$content;
return $returned_content;
}
add_shortcode('ft_snippet_thanks','ft_snippets_shortcode_thanks');
Hope this helps anyone who's looking to develop this further...
EDIT: ohh and here's an example of the thankyou page shortcode
Code:
[ft_snippet_thanks title="Thanks for your enquiry!"]<p>Your message has been submitted and we will get back to you shortly.</p>[/ft_snippet_thanks]