How can I pass a variable to wp_ajax action?

You’d need to put the variable’s value onto the page so that javascript can pass it back to PHP using ajax. Ajax functions are a completely separate “page load” so even if you made it global it won’t work. The best way to do this is using “wp_localize_script” (https://codex.wordpress.org/Function_Reference/wp_localize_script) Here’s an full example of how … Read more

Cannot load admin-ajax.php. No access-control allow origin*

There are filters for allowed_http_origins and add_allowed_origins. You can use them to set the proper Access-Control-Allow-Origin header in the response to your AJAX call. Add this to your theme’s functions.php file: add_filter(‘allowed_http_origins’, ‘add_allowed_origins’); function add_allowed_origins($origins) { $origins[] = ‘https://www.yourdomain.com’; return $origins; }

Basic question on passing variables to AJAX in WordPress

The simple answer: Don’t bother with the Admin AJAX API, use the REST API! Tell WordPress about your endpoint: add_action( ‘rest_api_init’, function () { // register dongsan/v1/whatever register_rest_route( ‘dongsan/v1’, ‘/whatever/’, array( ‘methods’ => ‘POST’, // use POST to call it ‘callback’ => ‘dongsan_whatever’ // call this function ) ); } ); Now we have an … Read more

Ajax and WP_Query/tax_query parameter

Your code looks fine, but it’s failing because your custom taxonomy is not getting registered anywhere in the action hooks of an AJAX call. My guess is that you are registering your custom taxonomies per the Taxonomies documentation, which tells you to add it to the “init” action hook. However, “init” doesn’t get called during … Read more

How do I filter posts by taxomony using AJAX

I figured it out! Here is the code I used: Add to functions.php: add_action( ‘wp_ajax_nopriv_load-filter2’, ‘prefix_load_term_posts’ ); add_action( ‘wp_ajax_load-filter2’, ‘prefix_load_term_posts’ ); function prefix_load_term_posts () { $term_id = $_POST[ ‘term’ ]; $args = array ( ‘term’ => $term_id, ‘posts_per_page’ => -1, ‘order’ => ‘DESC’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘yourtaxonomyhere’, ‘field’ => ‘id’, ‘terms’ => … Read more

How can I send data to admin-ajax via JS Fetch?

Is it proper way to pass the data in the form like I did? If you mean the body part (of your fetch() call), then yes, it is okay. However, You must send a query named action as part of the request (e.g. via the URL like example.com/wp-admin/admin-ajax.php?action=test), so that WordPress knows what AJAX action … Read more

Ajax request returning full page code

My issue was simple; jQuery.post(MyAjax.**ajax_url**, data, function(response) { was meant to be: jQuery.post(MyAjax.**ajaxurl**, data, function(response) { problem solved… sorry

Custom PHP endpoint for a plugin’s AJAX call

First of all, you shouldn’t call your own PHP file. You should use admin-ajax endpoint. Documentation On admin side, you could use ajaxurl to get URL for AJAX calls. On front side you have to declare by yourself variable with url. This is written in documentation: Note: Unlike on the admin side, the ajaxurl javascript … Read more