How to Include PHP ActiveRecord in WordPress as Global Feature
This post for developers who used to ORM/ActiveRecord and not familiar with WordPress Query (like me) and want the ORM/ActiveRecord to be available in WordPress.
As per post’s title, this post regarding how you can include PHP ActiveRecord for global use – let say you have multiple plugins that use PHP ActiveRecord, you can’t load same PHP ActiveRecord classes / redeclared classed in an application or system – to be precise in PHP.
So the solution is to add in PHP ActiveRecord in {wordpress_installation}/wp-includes
Configurations
- Download PHP ActiveRecord and extract to
{wordpress_installation}/wp-includes
- Create a new file in
{wordpress_installation}/wp-includes/php-activerecord/
calledAR.php
and use the codes below<?php class AR { static $conn; static $model_directories; public static function add_model_directories($list) { if(!is_array($list)) { return false; } if(!is_array(self::$model_directories)) { self::$model_directories = array(); } foreach ($list as $key => $value) { if(!in_array($value, self::$model_directories)) { self::$model_directories[] = $value; } } return true; } } function ar_init() { $connections = array( 'development' => 'mysql://'.DB_USER.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME, ); AR::$conn = ActiveRecordConfig::instance(); AR::$conn->set_model_directory(AR::$model_directories); AR::$conn->set_connections($connections); }
- Update
ActiveRecordConfig
class inphp-activerecord/lib/Config.php
for methodset_model_directory
andget_model_directory
. This to ensure we can include multiple model directories from multiple plugins.:public function set_model_directory($dir) { if( is_array($dir) ) { foreach( $dir as $k => $path ) { if( !file_exists($path ) ) { throw new ConfigException('Invalid or non-existent model directory: '.$path); } } } else if( $dir && !file_exists( $dir) ) { throw new ConfigException('Invalid or non-existent model directory: '. $dir); } $this->model_directory = $dir; } public function get_model_directory() { return $this->model_directory; }
- Include
ActiveRecord.php
andAR.php
in{wordpress_installation}/wp-settings.php
afterLine 155
:require( ABSPATH . WPINC . '/php-activerecord/ActiveRecord.php' ); require( ABSPATH . WPINC . '/php-activerecord/AR.php' );
- Add the following codes after
Line 241
to initialize PHP ActiveRecord:/** * Initialize PHP ActiveRecord */ ar_init();
How to Use in Your Plugin
- Create your WordPress Plugin (you can use wppb.me)
- Open up your WordPress Plugin main file and add in the following code in order to include your model directory in
AR
class:AR::add_model_directories(array( PLUGIN_PATH . 'includes' . DIRECTORY_SEPARATOR . 'models' ));
- Now create a sample model file which extend
ActiveRecordModel
in your plugin’s model directory. Following are the sample codes:<?php class GTState extends ActiveRecordModel { static $table_name = 'gt_states'; }
- Now you’re ready to use the
GTState
class in your view / controller.
I hope this trick will speed up the development progress.
Sorry WordPress Developers, I need a little hack to develop fast with PHP ActiveRecord, because I’m a beginner with WordPress, more things to learn.