Last active
October 1, 2019 21:17
-
-
Save ideag/5523992 to your computer and use it in GitHub Desktop.
A very basic framework for creating front-end forms for WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// a framework to handle frontend form submission | |
// written by Arūnas Liuiza | tinyStudio | wp.tribuna.lt | |
// Usage: use [tiny_form1] shortcode or get_tiny_form1()/the_tiny_form1() template tags | |
// Arguments: one (optional) argument 'redirect': pass url where to redirect after successful login (default: false); | |
// Localization: replace 'theme' with your text domain string. | |
// ======= FORM 1 =====> | |
// return form #1 | |
// usage: $result = get_tiny_form1(); | |
function get_tiny_form1($redirect=false) { | |
$return = "<form action=\"\" method=\"post\" class=\"tiny_form tiny_form1\">\r\n"; | |
// add as many inputs, selects, textareas as needed | |
$return .= " <p> | |
<label for=\"tiny_password\">".__('Password','theme')."</label> | |
<input type=\"password\" id=\"tiny_password\" name=\"password\"/> | |
</p>\r\n"; | |
// where to redirect on success | |
if ($redirect) | |
$return .= " <input type=\"hidden\" name=\"redirect\" value=\"{$redirect}\">\r\n"; | |
$return .= " <input type=\"hidden\" name=\"tiny_action\" value=\"form1\">\r\n"; | |
$return .= " <button type=\"submit\">".__('Login','theme')."</button>\r\n"; | |
$return .= "</form>\r\n"; | |
return $return; | |
} | |
// print form #1 | |
/* usage: <?php the_tiny_form1(); ?> */ | |
function the_tiny_form1($redirect=false) { | |
echo get_tiny_form1($redirect); | |
} | |
// shortcode for form #1 | |
// usage: [tiny_form1] in post/page content | |
add_shortcode('tiny_form1','tiny_form1_shortcode'); | |
function tiny_form1_shortcode ($atts,$content=false) { | |
$atts = shortcode_atts(array( | |
'redirect' => false | |
), $atts); | |
return get_tiny_form1($atts['redirect']); | |
} | |
// <============== FORM 1 | |
// ======= FORM 2 =====> | |
// return form #2 | |
// usage: $result = get_tiny_form2(); | |
function get_tiny_form2($redirect=false) { | |
$return = "<form action=\"\" method=\"post\" class=\"tiny_form tiny_form2\">\r\n"; | |
// add as many inputs, selects, textareas as needed | |
$return .= " <p> | |
<label for=\"tiny_password\">".__('Password','theme')."</label> | |
<input type=\"password\" id=\"tiny_password\" name=\"password\"/> | |
</p>\r\n"; | |
// where to redirect on success | |
if ($redirect) | |
$return .= " <input type=\"hidden\" name=\"redirect\" value=\"{$redirect}\">\r\n"; | |
// which action handler to use | |
$return .= " <input type=\"hidden\" name=\"tiny_action\" value=\"form2\">\r\n"; | |
$return .= " <button type=\"submit\">".__('Login','theme')."</button>\r\n"; | |
$return .= "</form>\r\n"; | |
return $return; | |
} | |
// print form #1 | |
/* usage: <?php the_tiny_form2(); ?> */ | |
function the_tiny_form2($redirect=false) { | |
echo get_tiny_form2($redirect); | |
} | |
// shortcode for form #1 | |
// usage: [tiny_form2] in post/page content | |
add_shortcode('tiny_form2','tiny_form2_shortcode'); | |
function tiny_form2_shortcode ($atts,$content=false) { | |
$atts = shortcode_atts(array( | |
'redirect' => false | |
), $atts); | |
return get_tiny_form2($atts['redirect']); | |
} | |
// <============== FORM 2 | |
// ============ FORM SUBMISSION HANDLER | |
add_action('init','tiny_handle'); | |
function tiny_handle() { | |
if (isset($_POST['tiny_action'])) { | |
switch ($_POST['tiny_action']) { | |
case 'form1': | |
// handle form #1 submission | |
// write data to db or perform other actions | |
// if actions vere successful | |
$success = true; | |
break; | |
case 'form2': | |
// handle form #2 submission | |
// write data to db or perform other actions | |
// if actions vere successful | |
$success = true; | |
break; | |
// add more cases if you have more forms | |
} | |
// if redirect is set and action was successful | |
if (isset($_POST['redirect']) && $_POST['redirect'] && $success) { | |
wp_redirect($_POST['redirect']); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment