Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

346
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda