This week for work, I’ve been creating a WordPress plugin to generate a contact form and send the e-mail via an AJAX call to a standalone script. However, because the script is standalone (i.e., not being used within the context of being called by WordPress itself) to get some of the information that the user configures in the plugin options, I need to include WordPress functionality.
The quick way to do this is to just include the wp-config.php file that is in the root directory of your WordPress installation. However, you have to include it in the main body of the standalone script – i.e., you can’t include it in a function or class (it will fail if you do this).
TL;DR: Just include this line at the very top of your standalone PHP script:
require_once(“../../../wp-config.php”);
This assumes that your plugin has its own directory within wp-content/plugins/. If you’re just using a single file in the plugins folder, take out the extra ../ at the beginning:
require_once(“../../wp-config.php”);