wp_ajax declaration confusing for Front end

I mean is_admin() is to check if you are on the administration so it will return false, so the code inside won´t be executed on the front-end, won´t it ? When you send an AJAX request using this method, you are sending it to wp-admin/admin-ajax.php, which is in the admin. This is explained in the … Read more

Why, if a function accepts arguments, it fails on ajax calls?

No arguments are passed to the AJAX callback function. Variables passed with the request are available in $_GET or $_POST. So if your function expects an argument, it can’t be used as the hooked callback directly. Your workaround is the correct way to use a function that accepts arguments in an AJAX request.

Load more posts in the same category – Ajax + Timber

I think there is a small correction in your code, you just passing page number but you have pass category ID also. I’ve added data-category attribute in load more button. So your archive.twig should like: <section class=”archive”> <p class=”archive__title h-headline text-size-l”>In the same thematic</p> <div id=”archive__list” class=”archive__list”> <article class=”post”> … </article> </div> </section> {% if … Read more

ajax form is returning the dreaded “[HTTP/1.1 400 Bad Request” and a zero

Your hook name uses an underscore: add_action( ‘wp_ajax_save_form’, ‘save_form’ ); add_action( ‘wp_ajax_nopriv_save_form’, ‘save_form’ ); But your action uses a hyphen. action: ‘save-form’ These need to match: action: ‘save_form’ Also, your AJAX callback appears to be in apps/save_signups.php, but you are only requiring that file inside the admin_menu hook: require_once ( ‘apps/save_signups.php’ ); //process submitted form … Read more

How to make a fetch() POST request to wordpress rest api?

‘X-WP-Nonce’ : Ajax.nonce Was missing that’s why it was giving the error fetch(‘https://mywebsite.online/wp-json/wp/v2/code’, { method: ‘POST’, credentials: ‘same-origin’, headers: new Headers({ ‘Content-Type’: ‘application/json;charset=UTF-8’, ‘X-WP-Nonce’ : Ajax.nonce }), body: JSON.stringify(OurPostData), }).then(response => { console.log(response); return response.json(); });

AJAX jQuery post frontend returning failed to load resource status 400

There’s two problems that I see. Firstly, your add_action(‘wp_ajax_nopriv_get_question’,… code is inside your template, but your template is not going to be loaded when the request is sent to wp-admin/admin-ajax.php, which does not load templates. The callback code and the code that hooks it need to go in your theme’s functions.php file. Secondly, you have … Read more