Help add ajax load more button

Your div wrapper is within your while loop, and split by your if clause. This cannot work properly, since your loop will generate up to 14 divs with the id ajax. Put the opening div tag after if ( $the_query->have_posts() ) { and the closing after wp_reset_postdata(); and you should be fine. Edit Are you … Read more

Admin Ajax and HTML5 Formdata

So it turns out the only problem was the header content type. By removing request.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded; charset=UTF-8′); and just leaving it out, it worked fine. That’s also with the form action being grabbed and appended. Ajax call: var formData = new FormData(form); formData.append(‘security’, WP.nonce); formData.append(‘action’, form.getAttribute(‘action’)); u.jax.post(WP.ajax, formData, onSent); Ajax Function: u.jax.post = function(url, data, … Read more

submitting form via admin-ajax.php returns 0

You don’t set an action in your form, so nothing is executed. Add a hidden field with action to the form: <input type=”hidden” value=”submit_filter” name=”action”> Also note that this: $postid = get_the_ID(); won’t work. This is a new request, so there is no post to get the ID from. You have to pass this with … Read more

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 speed up admin-ajax.php in wordpress

Here’s the answer you’re searching for: Ajax takes 10x as long as it should/could Unfortunately, the solution there involves very detailed knowledge of what parts to load / what not to load and why stuff would break. Reading through github issues / comments, it seems this doesn’t work with HTTPS. Otherwise, you really can’t speed … Read more

How to Globally Use wp_localize_script() Ajax URL

You can conditionally echo the code on only few templates or specific pages. Here is an example: add_action ( ‘wp_head’, ‘my_js_variables’ ); function my_js_variables(){ // for specific page templates $current_template = get_page_template(); // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php if( !isset($current_template) || ( … Read more

ajax_object is not defined when ajax calls the function

As Codex says, IMPORTANT! wp_localize_script() MUST be called after the script has been registered using wp_register_script() or wp_enqueue_script(). add_action( ‘init’, ‘my_script_enqueuer’ ); function my_script_enqueuer() { wp_enqueue_script( ‘jquery’ ); # before enqueue,if not register then register wp_enqueue_script( ‘add-order-front’ ); wp_localize_script( ‘add-order-front’, ‘ajax_object’, array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ) ) ); }