How to integrate wordpress with custom PHP script or application

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on email

In this short tutorial we will look into a method of integrating a custom PHP web application with wordpress.

The ideal way to integrate/include any SAAS or web based application with wordpress is by making or using the wordpress plugin mechanism and have some kind of API integration.

In this section we are going to use a simpler method to use WordPress functionality in a PHP file that exists outside of your WordPress installation . This is also a good approach for prototyping or quickly verifying the integration points.


Use WordPress functions outside WordPress files

Apparently the key to access wordpress functionality inside any custom php application is to include the wp_load.php file. This file is by default located in the base wordpress directory.

What exactly is in this wp_load.php file?

This file wp_load.php contains all of the core WordPress functionality. This includes the theme files, all the files of active plugins etc. This means the custom application will inherit all the initialization code, activation of plugins etc. This may not seem much if the wordpress install is basic. But as soon as you have wordpress loaded with several plugins, each plugin initialization will add to the time it takes before your custom php app can access

Plugins like WooCommerce Product Table, for e.g can quickly help bring custom SQL data into a wordpress table.

In this scenario we will place the custom php script or app inside the wp-content folder.

Location of wp-load.php file

We can simply hard code the location or file path to the wp_load.php. But this would not be idea for any app or script that you plan to distribute.

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );    

require_once( $parse_uri[0] . 'wp-load.php' );

The above code snippet provides a generic way to include the wp-load.php file. The first line takes the path of your php script and breaks it up at the wp-content folder name. The first portion of the array is the base wordpress path. This is where the default wp-load.php file is located.

Pros of including wp_load.php file

This is the quickest method to bring wordpress functionality within any custom app within the wordpress directory structure or outside. For e.g. we can now use the wordpress function to get the logged user id and verify the appropriate role that the user has with the following code

	$current_user = wp_get_current_user();
	if ( 0 == $current_user->ID ) {
		return false;
	}

	if (!in_array('administrator',  wp_get_current_user()->roles)) return false;
	else return true;

In the above code wp_get_current_user() is wordpress core function to get the user object. The ID will be a non-zero value for a real logged in user. In the second portion we check if the user has the administrator role as part of the many roles the user may have been assigned.

Cons of including wp_load.php file

It would be a bad idea to include wp-load.php file into each and every of your custom php application. This strategy would inlcude severe latency into your web application. If the idea is to use wordpress login mechanism or verifying if the user is logged in and getting the user role and other information then the best would be to have an entry point in your app that does this validation in the beginning and caches the information into a COOKIE.

The Core Tools

Create dashboard for any Database

Data Visualizer and Dashboard Application
SALE
This is the best dashboard software for its price. One good thing we did was to hire their consulting services to build few dashboard prototypes and provide some quick dashboard training.
- Terry Seal, IL
We evaluated Xcelsius and Qlikview and the cost for organization to implement dashboards was quoted over 10,000 USD. For fraction of the above quoted price, we were able to buy the licenses for the web based dashboard software and get some free training. This is truly a dashboard software for small businesses like us.
IT Manager of a Trucking company, OH