Skip to content

Instantly share code, notes, and snippets.

@ken-muturi
Created November 17, 2013 05:13
Show Gist options
  • Save ken-muturi/7509562 to your computer and use it in GitHub Desktop.
Save ken-muturi/7509562 to your computer and use it in GitHub Desktop.
Wordpress ajax calls
var form = $('#form'),
url = crimesmap_ajax.ajaxurl;
$.post(
url,
{
'postCommentNonce' : crimesmap_ajax.postCommentNonce,
'action' : crimesmap_ajax.action_call,
'data' : $('#form').serializeArray(),
},
function(ret) {
}
, 'json');
<?php
add_action( 'wp_enqueue_scripts', 'so_enqueue_scripts' );
function so_enqueue_scripts()
{
wp_register_script( 'ajaxHandle', get_template_directory_uri() . '/assets/js/ajax.js', array(), false, true );
wp_enqueue_script( 'ajaxHandle' );
wp_localize_script( 'ajaxHandle', 'this_theme_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'postCommentNonce' => wp_create_nonce( 'uk_crimemaps_nonce' ),
'action_call' => 'myaction',
) );
}
add_action( 'wp_ajax_neighbours_action', 'neighbours_wp_ajax_function' );
add_action( 'wp_ajax_nopriv_neighbours_action', 'neighbours_wp_ajax_function' );
function neighbours_wp_ajax_function()
{
//mark headers to have a json reponse
header("Content-type: application/json");
$force = isset($_POST['force']) ? $_POST['force'] : 'leicestershire';
$neighbourhoods = Ukcrimemap::neighbourhoods($force);
echo json_encode($neighbourhoods);
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment