Skip to content

Instantly share code, notes, and snippets.

@idangoldman
Created December 23, 2016 22:31
Show Gist options
  • Save idangoldman/41e2908d29ff3d09fba3c7aa251dc0a7 to your computer and use it in GitHub Desktop.
Save idangoldman/41e2908d29ff3d09fba3c7aa251dc0a7 to your computer and use it in GitHub Desktop.
<?php
/**
* The template for displaying all resource pages
*
* Template Name: Resource
* @package WordPress
* @subpackage Twenty_Sixteen
* @since Twenty Sixteen 1.0
*/
get_header(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
<h1>
<?php echo get_the_title(); ?>
</h1>
<article>
<?php echo get_the_content(); ?>
</article>
<form id="resource-form" action="http://www.staticpages.info" method="POST">
<input type="text" name="first_name" placeholder="First Name" required>
<input type="text" name="last_name" placeholder="Last Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="text" name="company" placeholder="Company" required>
<input type="hidden" name="resource_title" value="<?php echo get_the_title(); ?>">
<!-- Playing it safe with adding a nonce field -->
<?php wp_nonce_field( 'resource_form', 'resource_form_nonce' ); ?>
<!-- the ID needed because at the other end of the AJAX call PHP doesn't know the id of the page -->
<input type="hidden" name="post_id" value="<?php echo the_ID(); ?>">
<input type="submit" value="Send Me the Resource">
</form>
<script>
jQuery( document ).ready(function( $ ) {
$('#resource-form').submit(function( event ) {
var formData = $(this).serialize(); // jQuery's method of aggregating form's data
var action = '&action=resource_form'; // suffix of "wp_ajax_nopriv" action hook in functions.php
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php', // WordPress's url for AJAX calls, should come as a javascript variable from WordPress admin.
data: formData + action,
success: function( response ) {
// server's response goes here
$('#resource-form').replaceWith('<p>' + response.data + '</p>');
}
});
// prevent form submittion and page refresh.
event.preventDefault();
});
});
</script>
<?php endwhile; ?>
<?php get_footer(); ?>
<?php
// ########################
// functions.php
// ########################
add_action( 'wp_ajax_resource_form', 'zapier_webhook_request' ); // For logged in users.
add_action( 'wp_ajax_nopriv_resource_form', 'zapier_webhook_request' ); // For all the others.
function zapier_webhook_request() {
// Checking if the nonce is alright.
if ( isset( $_POST['resource_form_nonce'] ) &&
wp_verify_nonce( $_POST['resource_form_nonce'], 'resource_form' ) ) {
$post_id = intval( $_POST['post_id'] );
$resource_url = get_post_meta( $post_id, 'resource_url', true );
$zapier_webhook_url = get_post_meta( $post_id, 'zapier_webhook_url', true );
// Organazing and sanitize data before sending it to Zapier.
$payload = array(
'first_name' => sanitize_text_field( $_POST['first_name'] ),
'last_name' => sanitize_text_field( $_POST['last_name'] ),
'email' => sanitize_text_field( $_POST['email'] ),
'company' => sanitize_text_field( $_POST['company'] ),
'resource_title' => sanitize_text_field( $_POST['resource_title'] ),
'resource_url' => sanitize_text_field( $resource_url )
);
// Sending the data to Zapier,
// For wp_remote_post to work PHP cURL extension should be enabled on the server.
$response = wp_remote_post( $zapier_webhook_url, array( 'body' => $payload ) );
// Checking for errors and returning a json response to the AJAX call.
if ( ! is_wp_error( $response ) ) {
wp_send_json_success('Thanks, now check your email!');
} else {
wp_send_json_error('Oops, something went wrong. Try again later.');
}
} else {
wp_send_json_error('Nonce is not right.');
}
wp_die();
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment