I'm developing a rather simple plugin using the Wordpress Plugin Boilerplate. Utilizing AJAX, I set up a action based upon a button press that's supposed to remove an item from the custom database table I set up. The AJAX works, the button works, the call to the operating PHP file works. The call, located in my plug-in's admin partials folder where most of the plug-ins operations take place, looks like so ("txtHint" is an artifact of the tutorial I used):

function ajax_function(str) {
 var parsed = str.slice(19);
 var xmlhttp = new XMLHttpRequest();
 xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementById("txtHint").innerHTML = this.responseText;
 xmlhttp.open("GET", "http://" + window.location.hostname + "/wp-content/plugins/plugin_name/admin/partials/trigger_file.php?q=" + parsed, true);

However, when I get to the operating PHP file (located in the same directory) where some simple database manipulation is supposed to take place, nothing happens. The file at this point consists of:

global $wpdb;
$table_name['database_name'] = $wpdb->prefix . 'database_name';
echo var_dump($wpdb);
echo var_dump($table_name);
echo var_dump($wpdb->prefix);

That's it right now. And these var dumps come back as "NULL," "database_name", and "NULL."

What am I doing wrong here? In the few others files involved in this project everything works fine. What did I break? If it's an AJAX thing and Wordpress handles AJAX differently, I'd love to see a good tutorial for it, because the few I've found that handle Wordpress AJAX explicitly have been outdated and/or broken.

As an aside, this is my first plugin and I'm very, very new to PHP, Javascript, the whole 9 yards. Please, it would be very helpful if any responses could be dumbed down for me.

  • 1
    It looks like you're just loading a single (isolated) PHP file from a plugin, but you need the WordPress core loaded to be able to access $wpdb, so it's better to use hooks for admin-ajax.php or even check out the REST-API.
    – birgire
    Jan 8, 2018 at 14:44
  • 😱 you should never make direct calls to PHP files inside a theme or plugin, it's a massive security problem. There are APIs for making JS requests that avoid all of the issues you're encountering, just create a REST API endpoint and save yourself the pain
    – Tom J Nowell
    Jan 8, 2018 at 15:01
  • Both of you have fantastic advice, but this raises another concern: REST API? Where do I start with that?
    – Soilborn
    Jan 8, 2018 at 15:04
  • @Soilborn -- you can start REST API development with the REST API Handbook.
    – Pat J
    Jan 8, 2018 at 15:21

1 Answer 1


As stated in the Comments, do not access a plugin file directly. Instead, use other means that incorporate the REST API or the included Ajax functionality in Wordpress. To learn how to use Ajax in your Plugin, have a look here: Ajax in Plugins

  • That's a nice tutorial. Unfortunately, copying the code verbatim from the tutorial resulting in its built-in failure message, and nothing works. And with my current skill level with Javascript, PHP, etc. the REST API is going over my head. Is it really this hard to get an HTML button to freaking speak with a PHP function?
    – Soilborn
    Jan 8, 2018 at 15:59
  • Sorry, i don't mean to be offensive, but it seems to me as if you try to run before you can walk. Please be sure you understand how wordpress and wordpress plugins work by "going to the basics" and read this article: codex.wordpress.org/Writing_a_Plugin . I know that this is not "copy this here and it will work". Get a simple plugin from the repository and look into it to see how it does what it does. And props to you for trying to get into programming! Wordpress is not that hard to get into, and we are here to help you with that way! Jan 8, 2018 at 16:21
  • No offense taken. Prior to a week and a half ago, I didn't know anything about any of this, but then a project was assigned to me that forced me to get this all done, a sort of trial by fire. A coworker helped me out, and I should be good for presentation tomorrow, at which point I can crack the books and figure out what half of what I coded does. For the record, I'm not new to programming, just web development, but I appreciate the encouragement all the same!!
    – Soilborn
    Jan 8, 2018 at 20:06

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.