The following warnings occurred:
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 783 errorHandler->error
/showthread.php 26 require_once
Warning [2] Undefined array key "avatartype" - Line: 783 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 783 errorHandler->error
/showthread.php 26 require_once
Warning [2] Undefined variable $newpmmsg - Line: 40 - File: global.php(841) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/global.php(841) : eval()'d code 40 errorHandler->error
/global.php 841 eval
/showthread.php 26 require_once
Warning [2] Undefined array key "style" - Line: 909 - File: global.php PHP 8.1.27 (Linux)
File Line Function
/global.php 909 errorHandler->error
/showthread.php 26 require_once
Warning [2] Undefined property: MyLanguage::$lang_select_default - Line: 5024 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 5024 errorHandler->error
/global.php 909 build_theme_select
/showthread.php 26 require_once
Warning [2] Undefined array key "additionalgroups" - Line: 7162 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 7162 errorHandler->error
/inc/functions.php 5044 is_member
/global.php 909 build_theme_select
/showthread.php 26 require_once
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1370 fetch_forum_permissions
/showthread.php 137 forum_permissions
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key 1 - Line: 1415 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1415 errorHandler->error
/inc/functions.php 1380 fetch_forum_permissions
/inc/functions.php 2909 forum_permissions
/showthread.php 621 build_forum_jump
Warning [2] Undefined array key "mybb" - Line: 1952 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 1952 errorHandler->error
/inc/functions_indicators.php 41 my_set_array_cookie
/showthread.php 629 mark_thread_read
Warning [2] Undefined property: MyLanguage::$ratings_update_error - Line: 5 - File: showthread.php(732) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/showthread.php(732) : eval()'d code 5 errorHandler->error
/showthread.php 732 eval
Warning [2] Undefined array key "additionalgroups" - Line: 7162 - File: inc/functions.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions.php 7162 errorHandler->error
/inc/functions_user.php 844 is_member
/inc/functions_post.php 406 purgespammer_show
/showthread.php 1070 build_postbit
Warning [2] Undefined array key "profilefield" - Line: 6 - File: inc/functions_post.php(474) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/inc/functions_post.php(474) : eval()'d code 6 errorHandler->error
/inc/functions_post.php 474 eval
/showthread.php 1070 build_postbit
Warning [2] Undefined array key "canonlyreplyownthreads" - Line: 660 - File: inc/functions_post.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions_post.php 660 errorHandler->error
/showthread.php 1070 build_postbit
Warning [2] Undefined array key "showimages" - Line: 741 - File: inc/functions_post.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions_post.php 741 errorHandler->error
/showthread.php 1070 build_postbit
Warning [2] Undefined array key "showvideos" - Line: 746 - File: inc/functions_post.php PHP 8.1.27 (Linux)
File Line Function
/inc/functions_post.php 746 errorHandler->error
/showthread.php 1070 build_postbit
Warning [2] Undefined array key "invisible" - Line: 1506 - File: showthread.php PHP 8.1.27 (Linux)
File Line Function
/showthread.php 1506 errorHandler->error
Warning [2] Undefined variable $threadnotesbox - Line: 30 - File: showthread.php(1533) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/showthread.php(1533) : eval()'d code 30 errorHandler->error
/showthread.php 1533 eval
Warning [2] Undefined variable $addremovesubscription - Line: 79 - File: showthread.php(1533) : eval()'d code PHP 8.1.27 (Linux)
File Line Function
/showthread.php(1533) : eval()'d code 79 errorHandler->error
/showthread.php 1533 eval



FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wordpress shortcode/snippet experiment
#1
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:
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]
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:
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 ) {
    
extractshortcode_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;

    
extractshortcode_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;

    
extractshortcode_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 ) {
    
extractshortcode_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;

    
extractshortcode_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();

    
extractshortcode_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();

    
extractshortcode_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'); 
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

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]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)