Skip to content

Instantly share code, notes, and snippets.

@beardedtim
Last active March 30, 2016 18:14
Show Gist options
  • Save beardedtim/7ae0cb46efea941baeca to your computer and use it in GitHub Desktop.
Save beardedtim/7ae0cb46efea941baeca to your computer and use it in GitHub Desktop.

A basic template for ajax calls

CHECK YOUR FUCKING SPELLING YA DUMB ASS

if it returns 0

ACtually, just fucking check your spelling

add_action('wp_ajax_testFunction', 'testFunction');
add_action('wp_ajax_nopriv_testFunction', 'testFunction');
function testFunction() {
?>
<p>I WAS CALLED!</p>
<?php
$p= $_GET['id'];
?>
<p>And I got <?php echo $p; ?> for id</p>
<?php
die;
}
$( "#tagForm" ).submit(function( event ) {
var tag = event.target.tag.value;
check(tag);
event.preventDefault();
$.ajax({
type: "GET",
url: "/wp-admin/admin-ajax.php", // check the exact URL for your situation
dataType: 'html',
data: ({ action: 'testFunction', id: tag}),
success: function(data){
$('#test').html(data);
},
error: function(data)
{
alert("Your browser broke!");
return false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment