I am trying to start a PHP function in Wordpress on the click of a link. I've managed to do it using a form-submit, but I want to do it on a link click.
I've pulled together an AJAX request but it doesn't seem to work - any thoughts much appreciated.
HTML Link Code
<a href="#link_id"> Ajax Click </a>
Javascript Function
<script>
// Create a function to pick up the link click
$('#link_id').click(function(event){
event.preventDefault(); // prevent default behaviour of link click
var data = {
action: 'test_response_php', // This is the PHP function to call - note it must be hooked to AJAX
};
// Post to The Wordpress URL
jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function(response) {
alert(response);
});
});
PHP Function
function php_function() {
echo 'Hello World';
// Some interesting server side stuff
}
add_action('wp_ajax_nopriv_test_response_php', 'php_function');
add_action('wp_ajax_test_response_php', 'php_function');
wp_ajax_hooks should end in anexit