WordPress is a very powerful and flexible content management system. It can be as much complex and as much simple as you can imagine. It can be transformed into a fully functional eCommerce site with a cart and payment option on site and can be used as a personal blog after a few clicks of installation.

I recently worked with a client who asked me to turn his WordPress site into an affiliate website which will fetch feeds from different merchants and will display the products in her site. During the development I came across certain obstacles where using custom post types or page template wasn’t a good implementation of what was required. To overcome this problem I loaded WordPress API outside the blog to get the same database and look & feel but with my own programming logic to get the things done.

As I am pretty much done with the project I though of sharing my experience with you the developers or the WordPress users out there to solve you problems if you get one along your way.

Importing WP-LOAD.PHP

To create an API outside the wordpress installation you’ll need to import wp-load.php which resides inside your wordpress installation at root level.

Load wordpress outside blog through wp-load.php import

wp-load.php resides at root level of your wordpress installation

 

So we have figured out the place of our wp-load.php file and we will include this file into our custom.php file to import WordPress API outside. To get this done, add this code at the top of the custom.php file:

 

 

The first line, php comment shows what we are doing, the second line tells the wordpress API not to include templates and the third line imports the might, WordPress API. Please ignore the syntax of require function as the folder I was having for this tutorials have a space as well as capital letters since its my local sandbox and I wasn’t aware that someday I might have to create a WordPress tutorial around it, I never though of naming it conventionally. You’ll simply put the relative or absolute path to your wp-load.php file.

Now to put up as an example we’ll display a post in our fetching from the wp blog. To accomplish this task, we’ll write code in our custom.php file as follows:

 

 

query_posts is a native WordPress function, use to fetch posts, you can read more about it at WordPress codex. The argument showposts=1 will fetch one post and then somewhere down in our file we can show this post via this code:

 

If you are familiar with WordPress themes, created, modified a few or if you have ever got a chance to have a look at your theme files from the theme editor inside the admin dashboard. You might have seen this default WordPress loop a few times. Yes, the code will remain the same even outside the wp and this all is coming from the heart of WordPress, the wp-load.php file.

Some More:

You can even load your wordpress site in some other website if both of your sites resides on the same hosting space/server by providing the require function absolute path to your wp-load.php file.