Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, I got it using the following code:</p> <p>In main PHP plugin file:</p> <pre><code>echo "&lt;script src='URL PATH TO/ajax_file.js' type='text/javascript'&gt;&lt;/script&gt;"; add_action( 'wp_ajax_foobar', 'foobar_handler' ); add_action( 'wp_ajax_nopriv_foobar', 'foobar_handler' ); function foobar_handler() { // Handle request then generate response echo "ANYTHING..."; // do stuff here die(); // avoids extra 0 at the end of the response } </code></pre> <p>In ajax_file.js: (referred to above in script tag)</p> <pre><code>jQuery(document).ready(function($) { $('#id_to_submit_button').click( function() { var data = { action : 'foobar', Whatever : '1234', } ; // ajaxurl only needed for front - facing public pages, comment out for back end pages var ajaxurl = 'http://SITE_URL/wp-admin/admin-ajax.php'; jQuery.post(ajaxurl, data, function(response) { alert('Got this from the server: ' + response); } ); } ); } ); </code></pre> <p>As soon as I included the script in this way it worked, it would not when placed in a PHP function as many sources suggest. I tried soooo many other ways that should have worked. I went step by step on the Wordpress codex pages (many times) and several other tutorials. Enqueueing scripts, registering scripts, following all recommended practices. I still don't understand why this way works for me and all the other "correct" ways didn't. Could be something in my local server environment. Big thanks to those who answered and tried to help. God knows I have gotten the quick answer on StackOverflow enough times, I hope this saves someone the long hours I spent testing and troubleshooting...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload