Some background: I developed a wordpress plugin with a form in it. I need a way to notify the user if they mess up filling out the form. My initial inclination was php session variables. I added the various pieces of code to my plugin to make this work including the session_start()
at the top of my header which broke things. So i started doing research on the best way to show messages to the user.
I can use cookies as an alternative to php sessions but my question is:
What is the best way to set up front-end messages inside of Wordpress? I am new to wordpress mind you. I heard of Wordpress global variables? Does Wordpress provide something like global variables I can set which can be accessed anywhere? That would work. Is there some plugin i should look into (I would hate to bloat this). Are cookies the best route?
Here is a snippet of code that may illustrate what I am trying to accomplish:
Front End Form (shortcode)
<form method='POST'class="kb_donation_form" action="<?= // my action ?>">
<div class="form-group">
<label for="kb_first_name">First Name</label>
<input type="text" class="form-control" id="kb_first_name" name="kb_first_name" placeholder="First Name">
</div>
<div class="form-group">
<label for="kb_last_name">Last Name</label>
<input type="text" class="form-control" id="kb_last_name" name="kb_last_name" placeholder="First Name">
</div>
<div class="form-group">
<label for="kb_email">Email</label>
<input type="email" class="form-control" id="kb_email" name="kb_email" placeholder="Email Address">
</div>
<div class="form-group">
<label for="kb_grad_year">Graduation Year</label>
<select id="kb_grad_year" name="kb_grad_year" class="form-control">
<option value="friend">I am just a friend.</option>
<option value="1950">1950</option>
</select>
</div>
<div class="form-group">
<label for="kb_donation_amount">Donation Amount</label>
<input type="text" class="form-control" id="kb_donation_amount" name="kb_donation_amount" placeholder="100.00">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Make Donation" class="btn btn-default">
</div>
</form>
The post handler code:
if(isset($_POST['submit'])) {
if(
!empty($_POST['kb_first_name']) &&
!empty($_POST['kb_last_name']) &&
!empty($_POST['kb_email']) &&
!empty($_POST['kb_grad_year']) &&
!empty($_POST['kb_donation_amount'])
) {
// All Data Is Set
// Make sure email is valid format
$email = filter_var($_POST['kb_email'], FILTER_VALIDATE_EMAIL);
if(!empty($email)) { // ect.. ect..}
else {
// screw up
// Notify the user
// $_SESSION['error'] = 'You Screwed Up' === NEED ALTERNATIVE
}