pentatonicfunk
3/9/2018 - 9:08 PM

LGF Shortcode Function

LGF Shortcode Function

// SHORTCODE
function leadgen_form( $atts ) {

	extract( shortcode_atts( array(
		'styles'	=> true,
		'name'		=> 'Name',
		'phone'		=> 'Phone',
		'email'		=> 'Email',
		'budget'	=> 'Desired Budget',
		'message'	=> 'Message',
		'submit'	=> 'Submit'
	), $atts ) );

	if ( $styles === true ) {
		$class = ' class="leadgen-ui"';
	} else {
		$class = '';
	} ?>

	<div id="leadgen-form">
		
		<form id="leadgen_new_customer"<?php echo $class; ?> name="leadgen_new_customer" method="post" action="">

			<p><label for="lgf-title"><?php echo $name; ?></label>
			<input type="text" id="lgf-title" value="" tabindex="1" size="20" name="title" />
			<label id="lgf-title"><?php echo $errors['title'] ;?></label>
			</p>

			<p><label for="lgf-phone"><?php echo $phone; ?></label>
			<input type="tel" id="lgf-phone" value="" tabindex="1" size="20" name="leadgen_customer_phone" />
			</p>

			<p><label for="lgf-email"><?php echo $email; ?></label>
			<input type="email" id="lgf-email" value="" tabindex="1" size="20" name="leadgen_customer_email" />
			</p>

			<p><label for="lgf-budget"><?php echo $budget; ?></label>
			<input type="number" id="lgf-budget" value="" tabindex="1" size="20" name="leadgen_customer_budget" />
			</p>
			
			<p><label for="lgf-description"><?php echo $message; ?></label>
			<textarea id="lgf-description" tabindex="3" name="description" cols="50" rows="6"></textarea>
			</p>
			
			<p><input type="submit" value="<?php echo $submit; ?>" tabindex="6" id="submit" name="submit" /></p>
			
			<input type="hidden" name="action" value="leadgen_new_customer" />
			<?php wp_nonce_field( 'leadgen-new-client', 'leadgen_nonce' ); ?>

		</form>
		
	</div>

	<?php
	if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "leadgen_new_customer" ) {

		if ( ! isset( $_POST['leadgen_nonce'] ) || ! wp_verify_nonce( $_POST['leadgen_nonce'], 'leadgen-new-client' ) ) {
			
			print 'Sorry, your nonce did not verify.';
			
			exit;
		
		} else {
			
			$customer = $phone = $email = $budget = $message = '';
			
			if ( isset( $_POST['title'] ) && $_POST['title'] != '' ) {
				$customer =  $_POST['title'];
			} else {
				$errors['title'] = 'Title is required';
			}

			if ( isset( $_POST['leadgen_customer_phone'] ) && $_POST['leadgen_customer_phone'] != '' ) {
				$phone = $_POST['leadgen_customer_phone'];
			} else {
				echo '<p>Please, enter a valid number.</p>';
			}

			if ( isset( $_POST['leadgen_customer_email'] ) && $_POST['leadgen_customer_email'] != '' ) {
				$email = $_POST['leadgen_customer_email'];
			} else {
				echo '<p>Please, enter a valid email.</p>';
			}

			if ( isset( $_POST['leadgen_customer_budget'] ) && $_POST['leadgen_customer_budget'] != '' ) {
				$budget = $_POST['leadgen_customer_budget'];
			} else {
				echo '<p>Please, enter a valid budget.</p>';
			}
			
			if ( isset( $_POST['description'] ) ) {
				$message = $_POST['description'];
			}

			if ( !empty( $customer ) && !empty( $phone ) && !empty( $email ) && !empty( $budget ) ) {

				// Add the content of the form to $post as an array
				$new_client = array(
					'post_title'				=> $customer,
					'post_content'				=> $message,
					'leadgen_customer_phone'	=> $phone,
					'leadgen_customer_email'	=> $email,
					'leadgen_customer_budget'	=> $budget,
					'post_status'				=> 'publish',
					'post_type'					=> 'customers'
				);

				// Save the new post
				$pid = wp_insert_post( $new_client );
			}
		
		}
		
	}

}

add_shortcode( "leadgen", "leadgen_form" );