Skip to content

Instantly share code, notes, and snippets.

@jackreichert
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackreichert/3073e5ef2edf3f392a42 to your computer and use it in GitHub Desktop.
Save jackreichert/3073e5ef2edf3f392a42 to your computer and use it in GitHub Desktop.
(function($) {
$(document).ready(function(e){
e.preventDefault();
$('input[name=titleSubmit]').click(function(){
$.ajax({
type: "GET",
url: SSL_Ajax.ajaxurl,
cache: false,
dataType: "jsonp",
crossDomain: true,
data: {
action : 'ajaxSSL',
ajaxSSLNonce : SSL_Ajax.ajaxSSLNonce,
input : $('input[name=title]').val()
},
success: function( data ) {
console.log( 'success' );
console.log( data );
},
complete: function( data ) {
console.log( 'complete' );
console.log( data );
},
error: function( data ) {
console.log( 'error' );
console.log( data );
}
});
});
});
})(jQuery);
<?php
/**
* Plugin Name: Ajax SSL Example
* Plugin URI: http://www.jackreichert.com/
* Description: A simple plugin example that implements jsonp in WordPress.
* Version: 0.1
* Author: Jack Reichert
* Author URI: http://www.jackreichert.com
* License: GPL2
*/
add_action( 'wp_enqueue_scripts', 'ajaxSSL_scripts' );
add_action( 'wp_ajax_ajaxSSL', 'ajaxSSL_func' );
add_action( 'wp_ajax_nopriv_ajaxSSL', 'ajaxSSL_func' );
function ajaxSSL_scripts() {
wp_enqueue_script( 'ajaxSSL_script', plugins_url( 'ajax-ssl.js', __FILE__ ), array( 'jquery' ));
wp_localize_script( 'ajaxSSL_script', 'SSL_Ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxSSLNonce' => wp_create_nonce( 'ajaxSSL-nonce' ))
);
}
function ajaxSSL_func() {
$nonce = $_GET['ajaxSSLNonce'];
if ( ! wp_verify_nonce( $nonce, 'ajaxSSL-nonce' ) ) {
die ( 'Busted!');
}
// generate the response
$response = json_encode( $_GET );
// response output
header("content-type: text/javascript; charset=utf-8");
header("access-control-allow-origin: *");
echo htmlspecialchars($_GET['callback']) . '(' . $response . ')';
// IMPORTANT: don't forget to "exit"
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment