Has anyone tried putting PHP ActiveRecord on WordPress?

If you plan to use it just for your own code, and have it running alongside WordPress’ default database driver (wpdb), I see no real problems.

It’s only if you plan on fully integrating/overriding WP’s driver that I see it being near-impossible; hard-coded SQL is prolific throughout core, and translating them to ActiveRecord method calls would defeat any efforts for code optimisation.

FWIW, WordPress does support a driver override with db.php in your wp-content directory.

Update: For bringing PHP ActiveRecord into WordPress, I would suggest using a MU plugin. This keeps it out of the theme (where it doesn’t belong), out of WordPress core, and ensures that it is always loaded (Must Use plugins cannot be deactivated in the admin).

Copy the library to wp-content/mu-plugins/php-activerecord. Then in wp-content/mu-plugins/php-activerecord.php:

<?php

/**
 * Plugin Name: PHP ActiveRecord
 * Description: Load the PHP ActiveRecord library for use in plugins &amp; themes.
 * Version:     0.1
 * Author:      TheDeadMedic
 * Author URI:  http://wordpress.stackexchange.com/users/1685/thedeadmedic
 * Plugin URI:  http://wordpress.stackexchange.com/a/155530
 */

require_once dirname( __file__ ) . '/php-activerecord/ActiveRecord.php';

ActiveRecord\Config::initialize(
    function ( $cfg ) {
        $cfg->set_model_directory( '/path/to/your/model_directory' );
        $cfg->set_connections(
            array(
                'wordpress' => sprintf( 'mysql://%s:%s@%s/%s?charset=%s', DB_USER, DB_PASSWORD, DB_HOST, DB_NAME, DB_CHARSET ),
            )
        );

        $cfg->set_default_connection( 'wordpress' );
    }
);

Leave a Comment