Created
June 29, 2012 14:52
-
-
Save ekka21/3018426 to your computer and use it in GitHub Desktop.
Wordpress: Using wordpress call Ajax the right way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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