Dec 032011
 

One of my side projects powered by codeigniter required some data coming from a wordpress portal, I saw different opinions over the web, one of em said, I should place the entire wp app inside the ci app 😀 lol, why would I need to do that when all i need is data only and not functionality of wp dawg?!. So I ended up with this thing that I posted at the bottom. Technically, I just added another database config at my database.php file of the CI app

$active_group = "wp";
$active_record = TRUE;

$db['wp']['hostname'] = 'localhost';
$db['wp']['username'] = 'root';
$db['wp']['password'] = 'marvin';
$db['wp']['database'] = 'ci_db_wp';
$db['wp']['dbdriver'] = 'mysql';
$db['wp']['dbprefix'] = '';
$db['wp']['pconnect'] = FALSE;
$db['wp']['db_debug'] = TRUE;
$db['wp']['cache_on'] = FALSE;
$db['wp']['cachedir'] = '';
$db['wp']['char_set'] = 'utf8';
$db['wp']['dbcollat'] = 'utf8_general_ci';
$db['wp']['swap_pre'] = '';
$db['wp']['autoinit'] = TRUE;
$db['wp']['stricton'] = FALSE;


$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';';
$db['default']['username'] = 'root';
$db['default']['password'] = 'marvin';
$db['default']['database'] = 'ci_db';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

the last db settings will always be the default, while the one at the top is the 2nd db setting ( for wp ) . for some reasons, the persistent connection of the 2nd db setting should be set to FALSE to avoid unwanted output (according to some nerd over the web lol 😀 ). these db settings are determined by their group name => $active_group. Once, those settings are done.
we can already load the database and if there’s a model ,we can load it manually as well.

my controller sample snippet loading the WordPress model

	public function index()
	{
		
		$this->load->model('Wp_model');
		$data['wpost'] = $this->Wp_model->get_article_homepage();
                $data['title'] = 'Local Copy';
		$this->load->view('site_view',$data);
	}

my sample snippet for the wordpress model that loads the 2nd db setting

class Wp_model extends CI_Model
{
	public function __construct(){
		parent::__construct();
	}
	
	public function get_article_homepage(){
		$WP = $this->load->database('wp',TRUE);
		$query = $WP->query("SELECT post_content FROM wp_posts WHERE ID = 1");
		$row = $query->row();
		return $row->post_content;
	}
}

my sample view snippet

<?php echo $wpost; ?>

I guess , that’s it. A simple dev note for lazy assess :P….like me :))