WordPress 404 when using AJAX to call php function

When I was new to WP and PHP, a revelation occurred when I realized what PHP stands for. “PHP Hypertext Preprocessor.” Sure, the recursive acronym is cool, but the “preprocessor” was what rung my bell. In other words, when a page is served, the PHP runs first, generating HTML, which is then interpreted by the browser. So you can’t load a PHP file into your HTML doc and expect any interaction. It’s already done its work and gone to the bar by the time your page is loaded.

The solution? AJAX (but you know this). And the best way to get started with WP and AJAX is to read the Codex.