So i'll preface this with the fact that I am trying to learn Ajax and I've spent considerable time trying to get a single example to work but to no avail. In the long run it'll replace a lot of my MySQL website calls that are done via a submit button requiring a page refresh.

After a lot of frustration I've come to realise that there is likely an issue with the calling of the php script in the xmlhttp.open("GET","example.php?q="+str,true); call.

I use a php snippet (wordpress plug-in which allows the user to include a short code on a page instead of embedding php code). The snippet doesn't require you to add the <?php / ?> bookends and doesn't have the option to name the php script, is there a simple way to name the script within the php script itself?

Alternatively, where is the best place to store php scripts to be able to call them up, I'm assuming the functions.php file of which I have no experience with to this point.

Note I'd rather be able to continue using my current snippet plug-in if possible (PHP code snippets (Insert PHP)).

1 Answer 1


PHP snippets intended for shortcode use aren't going to work here (unless the plugin explicitly offers a solution, in which case ask them).

To properly use AJAX in WordPress you need to put the functions in your child theme's functions.php file or a simple plugin. Then you need to either:

  • Thanks for the quick response, I'll take a look. Appreciate the feedback.
    – Morts81
    Aug 6, 2018 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.