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

77
Views
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 answers
Answer question

0

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

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!