WordPress Form Validation with WordPress Error Class
X

Congrats, You are Subscribed to Receive Updates.

WordPress Form Validation with WordPress Error Class


WordPress Form Validation with WordPress Error Class  is one of the way to handle the Form validations without using jQuery and AJAX. Here is a short example with HTML form and WordPress validations.

WordPress-Form-Validation-with-WordPress-Error-Class

Let’s move to the code work. Create a sample form just like the following one.

<form name="kv-contact" id="kv-contact" method="post" action="" >
 	<table name="form_table_find_aPlate" >
		<tr> 
        	<td>Name <span class="star-form"> * </span></td>
            <td> <input  maxlength="150" size="30" title="" id="kv_persion_name" name="kv_persion_name" value="<?php echo $kv_persion_name ?>" type="text" /></td> 
        </tr>
		<tr> 
        	<td>Contact Number <span class="star-form"> * </span></td>
        	<td> <input  maxlength="150" size="30" title="" id="kv_persion_contact_no" name="kv_persion_contact_no" value="<?php echo $kv_persion_contact_no ?>" type="text" /></td> 
       </tr>
	   <tr> 
        	<td>Email <span class="star-form"> * </span></td>
            <td> <input  maxlength="150" size="30" title="" id="kv_persion_email" name="kv_persion_email" value="<?php echo $kv_persion_email ?>" type="email" /></td> 
        </tr>
		<tr> 
        	<td>Message <span class="star-form"> * </span></td>
            <td><?php $kv_editor_args =  array('media_buttons' => false , 'teeny' => true ); wp_editor( $kv_persion_extra_info, 'kv_persion_extra_info',  $kv_editor_args ); ?></td> 
        </tr>
		<tr>  
        	<td colspan="2" style="text-align: center;">
            	<input type="hidden" name="action" value="submit-a-plate" > 
                <input value="Submit" name="button_9" type="submit" /> 
            </td>  
        </tr>
	</table> 
</form>

Created a simple contact form. than start begin writing the php functions to handle the form data and send email.  The following code, check the form submission and stripping the slashes.

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "submit-a-plate") {
		$fields = array(
					'kv_persion_name',
					'kv_persion_contact_no',
					'kv_persion_email',				
					'kv_persion_extra_info'				
				);

		foreach ($fields as $field) {
			if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
		}
}

Now, lets begin to work with WordPress error class. Just create a object for Error Class.

$errors = new WP_Error();

Now we are going to add error array list, with the form returned data.  The following one helps you to get check the name field.

if ($posted['kv_persion_name'] != null )
			$kv_persion_name =  $_POST['kv_persion_name'];
		else 
			$errors->add('empty_title', __('<strong>Notice</strong>: Please enter your Name First.', 'kvcodes'));		

If  it returns empty, than the error will be added to the error list . Likewise we have to add all the errors in the error object.  And now we have to check the errors list before start sending the variable values.

		if ( !$errors->get_error_code() ) { 	
	
				$kv_info = nl2br($kv_persion_extra_info) ; 				
				$kv_email_msg = '<table style="width: 85% " > 					
					<tr>
						<td> Person Name 	: 		</td>  
						<td> '.  $kv_persion_name.'</td> 
					</tr>					
					<tr>
						<td> Contact Number : 		</td>  
						<td> '. $kv_persion_contact_no.'</td> 
					</tr>					
						<tr><td> Email :  				</td>  
						<td> '. $kv_persion_email.'</td> 
					</tr>										
					<tr>
						<td> Extra Info :  			</td>  
						<td> '. $kv_info .'</td> 
					</tr> 					
			   </table>' ;	 
			   
			$kv_head = ' From : '. $kv_persion_name.' <'.$kv_persion_email .'>'; 						
			$kv_subject ='Message from Megaplates Contact - '.	$kv_persion_name; 			
			 wp_mail('admin@yoursite.com',$kv_subject, $kv_email_msg, $kv_head );
}

The above code works to validate the error array, if the array is empty. it will be sent via email. If suppose one or more field is empty. than we need to show the errors to the user. Here i used the following code to display the errors.

<?php 
	if($sub_success == 'Success') {
		echo '<div class="success">' . __( 'Thank you we will get back you soon.', 'post_new' ) . '</div>';
		$sub_success = null;
	} 
	if (isset($errors) && sizeof($errors)>0 && $errors->get_error_code()) :
		echo '<ul class="errors">';
		foreach ($errors->errors as $error) {
			echo '<li>'.$error[0].'</li>';
		}
		echo '</ul>';
	endif; 	
?>

Also the $sub_success will be set here.

if (   wp_mail('admin@yoursite.com',$kv_subject, $kv_email_msg, $kv_head )    ) {
				$sub_success ='Success' ;
			}

That’s it for error management. Here is the full code for your reference.

<?php
/**
Template Name: Contact Us
*/
get_header();
$errors = new WP_Error();	

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "submit-a-plate") {
		$fields = array(
					'kv_persion_name',
					'kv_persion_contact_no',
					'kv_persion_email',				
					'kv_persion_extra_info'				
				);

		foreach ($fields as $field) {
			if (isset($_POST[$field])) $posted[$field] = stripslashes(trim($_POST[$field])); else $posted[$field] = '';
		}

   //  Validattion Check start here 
		if ($posted['kv_persion_name'] != null )
			$kv_persion_name =  $_POST['kv_persion_name'];
		else 
			$errors->add('empty_title', __('<strong>Notice</strong>: Please enter your Name First.', 'kvcodes'));		
		
		if ($posted['kv_persion_contact_no'] != null )
			$kv_persion_contact_no =  $_POST['kv_persion_contact_no'];
		else 
			$errors->add('empty_contact_no', __('<strong>Notice</strong>: Please enter your Contact Number.', 'kvcodes'));		
		if ($posted['kv_persion_email'] != null )
			$kv_persion_email =  $_POST['kv_persion_email'];
		else 
			$errors->add('empty_email', __('<strong>Notice</strong>: Please enter your Email.', 'kvcodes'));		
		
		if ($posted['kv_persion_extra_info'] != null )
			$kv_persion_extra_info =  $_POST['kv_persion_extra_info'];
		else 
			$errors->add('empty_msg', __('<strong>Notice</strong>: Please enter your Message.', 'kvcodes'));		
		
		if ( !$errors->get_error_code() ) { 	
	
				 //wp_mail( $to, $subject, $message, $headers, $attachments );
				 
	
				$kv_info = nl2br($kv_persion_extra_info) ; 				
				$kv_email_msg = '<table style="width: 85% " > 					
					<tr>
						<td> Person Name 	: 		</td>  
						<td> '.  $kv_persion_name.'</td> 
					</tr>					
					<tr>
						<td> Contact Number : 		</td>  
						<td> '. $kv_persion_contact_no.'</td> 
					</tr>					
						<tr><td> Email :  				</td>  
						<td> '. $kv_persion_email.'</td> 
					</tr>										
					<tr>
						<td> Extra Info :  			</td>  
						<td> '. $kv_info .'</td> 
					</tr> 					
			   </table>' ;	 
			   
			$kv_head = ' From : '. $kv_persion_name.' <'.$kv_persion_email .'>'; 						
			$kv_subject ='Message from Megaplates Contact - '.	$kv_persion_name; 			
			
			if (   wp_mail('info@megaplates.net',$kv_subject, $kv_email_msg, $kv_head )    ) {
				$sub_success ='Success' ;
			}
			
			 if ($sub_success == 'Success' ) {
				unset($kv_persion_name, $kv_persion_contact_no, $kv_persion_email,  $kv_persion_extra_info );
			} 
		}	
	}  // endif line no-8

?>

<div class="padding_article">

<div class="article-content" style="line-height:3em;">
<?php 
	if($sub_success == 'Success') {
		echo '<div class="success">' . __( 'Thank you we will get back you soon.', 'post_new' ) . '</div>';
		$sub_success = null;
	} 
	if (isset($errors) && sizeof($errors)>0 && $errors->get_error_code()) :
		echo '<ul class="errors">';
		foreach ($errors->errors as $error) {
			echo '<li>'.$error[0].'</li>';
		}
		echo '</ul>';
	endif; 	
?>
<h2>Contact US</h2>

<span style="color: #666;">Use the following form and draft your message here. </span> 		
<br>

 <form name="kv-contact" id="kv-contaact" method="post" action="" >
 	<table name="form_table_find_aPlate" >
		<tr> 
        	<td>Name <span class="star-form"> * </span></td>
            <td> <input  maxlength="150" size="30" title="" id="kv_persion_name" name="kv_persion_name" value="<?php echo $kv_persion_name ?>" type="text" /></td> 
        </tr>
		<tr> 
        	<td>Contact Number <span class="star-form"> * </span></td>
        	<td> <input  maxlength="150" size="30" title="" id="kv_persion_contact_no" name="kv_persion_contact_no" value="<?php echo $kv_persion_contact_no ?>" type="text" /></td> 
       </tr>
	   <tr> 
        	<td>Email <span class="star-form"> * </span></td>
            <td> <input  maxlength="150" size="30" title="" id="kv_persion_email" name="kv_persion_email" value="<?php echo $kv_persion_email ?>" type="email" /></td> 
        </tr>
		<tr> 
        	<td>Message <span class="star-form"> * </span></td>
            <td><?php $kv_editor_args =  array('media_buttons' => false , 'teeny' => true ); wp_editor( $kv_persion_extra_info, 'kv_persion_extra_info',  $kv_editor_args ); ?></td> 
        </tr>
		<tr>  
        	<td colspan="2" style="text-align: center;">
            	<input type="hidden" name="action" value="submit-a-plate" > 
                <input value="Submit" name="button_9" type="submit" /> 
            </td>  
        </tr>
	</table> 
</form>
</div>
</div> 
									
<?php 	get_footer();	?>

Any queries and doubts, comment below.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Congrats, You are Subscribed to Receive Updates.

Advertisement

Category Posts