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?
I figured this out in the end. Changing return to echo seemed to be the fix.