WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to send a ajax request in wordpress which tracks my clicks. For now i have added this in my functions file :

add_action('init', 'my_script_enqueuer');

  function my_script_enqueuer() {
    wp_register_script("history_script", get_template_directory_uri() . '/js/history_script.js', array('jquery'));
    wp_localize_script('history_script', 'myAjax', array('ajaxurl' => get_template_directory_uri().'/functions.php'));

  add_action("wp_ajax_history_trace", "history_trace");

  function history_trace() {
      echo 'fasfasgasgas'; die;

and in my js file :

jQuery(document).ready( function() {

   jQuery("#searchsubmit").click( function() { 
         type : "post",
         dataType : "json",
         url : myAjax.ajaxurl,
         data : {action: "history_trace"},
         success: function(response) {
            if(response.type == "success") {
            else {



But when i see my console the rquest appears in red color and there is no response. Please Help!!

share|improve this question
Read wordpress.stackexchange.com/questions/9231/… and use the correct AJAX URL. – toscho May 19 '13 at 9:24
Yes i waas trying to use it in functions..thanks.. – Sankalp Mishra May 19 '13 at 9:32

your ajax url is wrong i think. ajax link should be admin_url( 'admin-ajax.php' ). That means

wp_localize_script('history_script', 'myAjax', array('ajaxurl' => get_template_directory_uri().'/functions.php'));

will be this

wp_localize_script('history_script', 'myAjax', array('ajaxurl' => admin_url( 'admin-ajax.php' ));
share|improve this answer
Yes it is a misytake – Sankalp Mishra May 19 '13 at 9:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.