Including WordPress functions in a custom PHP file

In order to use your WordPress website’s functions in an external PHP file, you need to load your wp-load.php file accordingly.

Load your wp-load.php file

In your WordPress theme’s folder, create your custom PHP file and add the following line of code at the top:

require_once( '../../../wp-load.php' );

Note, if you’re on version-4 or version-5 (Gutenberg), the above code should work for you.

However, if you’re using a previous version of WordPress, you should add another parent directory path  (../, a total of 4-parent paths) into your require_once() function.

Finally, in order to navigate to your file, you should use the direct theme URL as per the example below:

example.com/wp-content/themes/example-theme/custom-file.php