Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
PHP Hooks add_action() do_action() are not working

My code works if I remove the hooks add_action() and remove 'action': 'contact' In AJAX. In this case it returns 'Success'. But once I add hooks to email.php and 'action': 'contact' it stops to return me anything. Could you give me an advice how to fix it? I believe I do it exactly as in the documentation.

This is my simple code:

Email.php:

<?php

 add_action( 'wp_ajax_nopriv_contact', 'contact' );
 add_action( 'wp_ajax_contact', 'contact' );

 function contact() {
  echo 'Success';
  wp_die();
 }

 ?>

Functions.php:

function load_script()
{
  wp_enqueue_script( 'contact_form', get_template_directory_uri() . '/contact_form/contact_form.js', array('jquery') );
  wp_localize_script( 'contact_form', 'ajax_object', array('ajaxurl' => get_template_directory_uri() . '/email.php')
  );
}

add_action('wp_enqueue_scripts', 'load_script');

Contact_form.js:

(function($){
$(document).ready(function() {
    $( '.mybtn' ).click( function(e) {
        e.preventDefault();
        $.ajax({
          url: ajax_object.ajaxurl,
          type: 'POST',
          data: {
              'action': 'contact'
          },
          success: function( response ) {
              console.log('The server responded: ',response);
          },
        });
   });
});
})(jQuery);

Then I tried just to remove 'action': 'contact' and change Email.php to:

<?php

add_action( 'contact', 'contactme' );

function contactme() {
  echo 'Success';
}

do_action('contact');

?>

And it doesn't work! But the next code works perfectly:

<?php
  echo 'Success';
?>

AJAX returns Success.

I also tried to add 10 and also 1 at the end of add_action:

<?php

add_action( 'wp_ajax_nopriv_contact', 'contact', 10 );
add_action( 'wp_ajax_contact', 'contact', 10 );

function contact() {
  echo 'Success';
  wp_die();
}

?>

But it hasn't helped.

Do you know why my hooks don't work? Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When using ajax in WordPress we do not call the file that stores the function you want to run. Instead admin-ajax.php is called.

Your email.php file will be as following, but you must include it in your function.php. Or just put this function inside the function.php file. Also note that every function which is going to be called by ajax request must end with wp_die().

function contact() {
  echo 'Success';
  wp_die();
}
add_action( 'wp_ajax_nopriv_contact', 'contact' );
add_action( 'wp_ajax_contact', 'contact' );

Next in load_script() add admin_url('admin-ajax.php') in 'ajaxurl'.

function load_script() {
  wp_enqueue_script( 'contact_form', get_template_directory_uri() . '/contact_form/contact_form.js', array('jquery') );
  wp_localize_script( 'contact_form', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php')) );
}
add_action('wp_enqueue_scripts', 'load_script');

contact_form.js will almost be the same.

(function($){
  $('.mybtn').on('click', function(e) {
    e.preventDefault();
    $.ajax({
      url: ajax_object.ajaxurl,
      data: {
        action: 'contact'
      },
      success: function(data) {
        console.log(data);
      },
    });
 });
})(jQuery);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!