Admin-ajax responds with 0 due to empty $_REQUEST

In end of Ajax function you need to die() function function example_frontpage_feed_ajax() { require_once ‘inc/fetch-feed.php’; $fetch_page = isset($_GET[‘page’]) ? intval($_GET[‘page’]) : 0; $fetch_next_time = isset($_GET[‘next_time’]) ? $_GET[‘next_time’] : null; header(‘Access-Control-Allow-Origin: https://www.example.com’); example_feed_content($fetch_page, $fetch_next_time); die(); // This is important in ajax } add_action(‘wp_ajax_nopriv_example_frontpage_feed’, ‘example_frontpage_feed_ajax’); add_action(‘wp_ajax_example_frontpage_feed’, ‘example_frontpage_feed_ajax’);

Most performant way of fetching remote API data?

The most performant way of fetching remote API data is not fetching it at all. Thus, use Transients API or WP Object Cache to save your computed results for future use and avoid calling external API (and further computations) on every subsequent request. Additionally, fetching, invalidating and regeneration of this data can be done in … Read more

Enqueue script dinamically

There’s a fundamental misunderstanding of what AJAX does. You cannot enqueue scripts directly from the PHP called from the javascript. You need to print something that can be used by the javascript making the AJAX call. You can then use javascript to add that file to the DOM. functions.php //* Enqueue Ajax call on wp_enqueue_scripts … Read more

How to declare a JS variable in an AJAX call

The right way is to post the data back to the WordPress admin ajax url then in your PHP function access them using the $_POST variables. Example jQuery function that passed a category id back to wordpress: function cat_edit_get() { jQuery( “#loading-animation”).show(); var catID = jQuery(“#cat :selected”).val(); var text = jQuery(“#cat :selected”).text(); jQuery(‘#replace’).html(text); jQuery.ajax({ type: … Read more

Using wp_handle_upload and media_handle_sideload with ajax

Uploading files in ajax is a bit tricky because it is not possible to upload files using the browser’s XMLHttpRequest object so you need to use some kind of Ajax upload plugin. Also wp_handle_upload() is not what your using in your code its media_handle_sideload() wp_handle_upload() – should be used for file uploads (input file field) … Read more

WordPress ajax get content post id

There are many errors and bad practices in your code. I’ve fixed most of them. Explaining all those here is not possible. I’ve written the explanation as comment where they need to. Please read the whole code and comments attentively- custom.js // Wrap any jQuery code like this. It makes ‘$’ use fullproof // and … Read more