Using WordPress Functions outside of WordPress

For one of the sites I manage there is something funny happening with the server time. The site uses WordPress and I really like the way WordPress $wpdb class works. In a few sites I manage I occasionally have scripts which preform routine functions which effect the content in WordPress. To use all of the WordPress functions you simply add ‘include_once(“wp-load.php”);’ to the top of you PHP script.

To track the time I created the following script and I have set up a CRON Job to run every minute to track the time on the server.

The PHP is:
[cc lang=”PHP”]include_once(“wp-load.php”);
global $wpdb;
$query=”INSERT INTO time_track (php_time) VALUES (‘”.date(‘Y-m-d H:i:s’).”‘)”;
$wpdb->query($query);[/cc]

By including the wp-load, I have full access to all of the WordPress functions. In this case I am using the $wpdb class which has reduced my code to 3 lines (could be 2 if I typed the query directly into the query function).

The table SQL is:
[cc lang=”SQL”]CREATE TABLE `time_track` (
`id` int(11) NOT NULL auto_increment,
`php_time` timestamp NULL default NULL,
`mysql_time` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1[/cc]

This is a great way to have stand alone scripts which interact with the WordPress install. I am sure that the wp-load is adding extra overhead, though it does not matter as only a CRON Job once a minute is calling the script.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s