Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

345
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda