title: AJAX request on link click in Wordpress
body: <p>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.</p>
  
  <p>I've pulled together an AJAX request but it doesn't seem to work - any thoughts much appreciated.</p>
  
  <p>HTML Link Code</p>
  
  <pre><code>&lt;a href="#link_id"&gt; Ajax Click &lt;/a&gt;
  </code></pre>
  
  <p>Javascript Function</p>
  
  <pre><code>&lt;script&gt;
  
  // 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("&lt;?php echo admin_url('admin-ajax.php'); ?&gt;", data, function(response) {
          alert(response);
      });
  
  });
  </code></pre>
  
  <p></p>
  
  <p>PHP Function</p>
  
  <p>
  
  <pre><code>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');
  </code></pre>