Skip to content

Instantly share code, notes, and snippets.

@ekka21
Created June 29, 2012 14:52
Show Gist options
  • Save ekka21/3018426 to your computer and use it in GitHub Desktop.
Save ekka21/3018426 to your computer and use it in GitHub Desktop.
Wordpress: Using wordpress call Ajax the right way
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'GET',//POST, JSON, XML
dataType: 'html',
data: ({
action: 'MY_AJAX_FUNCTION',
state: state,
}),
success: function(data){
if (data){
}
else
{
}
}
});
//Put this in function.php
wp_localize_script( 'ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
function MY_AJAX_FUNCTION(){
//doing something here
die($results);
}
add_action('wp_ajax_MY_AJAX_FUNCTION', 'MY_AJAX_FUNCTION');
add_action('wp_ajax_nopriv_MY_AJAX_FUNCTION', 'MY_AJAX_FUNCTION');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment