WordPress functions outside WordPress

Today I was working on a project where I needed to use WordPress functionality outside of WordPress. After searching the web, Thanks to this Stackoverflow question I found out that I needed to include the wp-load.php file to get this done.

To get the wp-load file, you need the path to it. So here came the next problem; how to find the path on a nice way instead of doing this:

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

Thanks to Frankie Jarret’s article “The simplest way to require/include wp-load.php” I was able to find the path on a clean coded way.

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php');

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.

Comments

    No comments yet..

Leave a reply

Feel free to ask questions or make comments.