WordPress and jQuery AJAX … and some PHP

I had an issue where I created a custom form in WordPress using jQuery, AJAX, and PHP. Here’s a quick rundown of the process.

I created a custom WP template file with form HTML included. I created a JS script in an external file (not sure if that mattered here.). The JS listens for the form submission button click and validates the required form fields, then if it passes, “posts” the form data to a PHP script.

Everthing was working up until the PHP file came into play. The JS was not finding the PHP file, according to the console log. I read a few things about WordPress and AJAX issues, but wasn’t sure it applied. It just wasn’t finding the file.

I do know that you can’t just run a PHP script if it’s in the theme folder. Files in that folder are handled differently than a file in the root of the site. I know this because I tested this all in a root-level directory, and it worked. And, if you place a PHP file in the theme directory and try to directly run it, it will not work.

What ended up working for me was moving my PHP file outside of the WP theme. I’m not 100% sure why this worked. Did it have anything to do with AJAX? I don’t think so.