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

83
Visualizações
WordPress ajax not returning what I expected

I'm using the snippets plugin along with Elementor. I have an ajax function set up as a snippet like this:

add_action( 'wp_ajax_get_slug_from_id', 'get_slug_from_id' );
add_action( 'wp_ajax_nopriv_get_slug_from_id', 'get_slug_from_id' );

function get_slug_from_id() {
    return url_to_postid( $_POST['slug'] ); 
    die();
}

And I'm embedding some jQuery on a page to try and retrieve the post IDs like this:

<script>
    (function($){
        
        $(window).on('load',function(){
        
            $('.ee-calendar-skin--default .ee-calendar__day__event__name a').each(function(){
                
                let slug = $(this).attr('href').match( /([^/]*)\/[^\/]*$/ )[1];
                
                $.post(
                    wp.ajaxurl, 
                    {
                        'action': 'get_slug_from_id',
                        'slug': slug
                    }, 
                    function(response) {
                        console.log(response);
                    }
                );
                
            });
        
        });
        
    })(jQuery);
</script>

This just returns the HTML of the page, and if I change wp.ajaxurl to '/wp-admin/admin-ajax.php' it just returns 0.

Any idea where I'm going wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I figured this out in the end. Changing return to echo seemed to be the fix.

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