Integrating CodeIgniter with WordPress
01 Dec 2010Recently I was working on a website for a small business client. They wanted a simple system to allow them to easily update a section of their website. Since it was a small website, I wanted to accomplish this without putting too much time but still creating something user-friendly and easy to use that can get the job done. The main site was made using CodeIgniter. So we decided to create a WordPress blog for the client where they could put their content and I would pull that content inside their CI website. Here’s how:
First of all, install WordPress in a folder called blog in your website. It would be located at the top level directory of the website. So it would be alongside the system folder of the CI installation. If you already have a DB for your site, have WordPress add tables to that same DB with a prefix to make things simple (although this is not a requirement).
Once that’s done. All you have to do is add the following line in the at the top of your CodeIgniter’s index.php file. Change the path to wp-blog-header.php as needed to point to your wordpress’s root directory.
<?php
require('blog/wp-blog-header.php');
One you get that dialed in, rest is pretty straight forward. All of WordPress’ native API functions should now be accessible to you. So you could throw in a WordPress loop in any of the CI views like for example:
Articles.php (View):
<?php
if ($type == 'Tag') { query_posts('tag='.$id.'&posts_per_page=-1'); }
else if ($type == 'Category') { query_posts('category_name='.$id.'&posts_per_page=-1'); }
else { query_posts('posts_per_page=-1'); }
while (have_posts()) : the_post(); // Loop
?>
<a href="<?=base_url();?>Research/Article/<?php the_ID(); ?>"><h4><?php the_title(); ?></h4></a>
By <?php the_author(); ?>
<p class="details"><? the_time('F jS, Y') ?></p>
<?php the_excerpt(); ?>
<hr/>
<?php
endwhile; // End Loop
?>
…and a sidebar on the same which which would list out all the tags and categories to select from:
<!-- SIDE BAR -->
<div id="sideBar">
<h3>Categories:</h3>
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => '0',
'style' => 'none',
'show_count' => '0',
'hide_empty' => '1',
'use_desc_for_title' => '1',
'child_of' => '0',
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'current_category' => '0',
'hierarchical' => 'true',
'title_li' => __( 'Categories' ),
'number' => NULL,
'echo' => '1',
'depth' => '0');
wp_list_categories( $args ); ?>
<br>
<h3>Tags:</h3>
<?php
$args = array(
'smallest' => '12',
'largest' => '12',
'unit' => 'px',
'number' => '45',
'format' => 'flat',
'separator' => ' ',
'orderby' => 'name',
'order' => 'ASC',
'exclude' => '',
'include' => '',
'link' => 'custom',
'taxonomy' => 'post_tag',
'echo' => true,
'link_url' => base_url()."Research/Articles/Tag/");
wp_tag_cloud( $args ); ?>
</div>
<!-- /SIDE BAR -->
Article.php (View):
<!-- CONTENT -->
<div id="mainContent">
<!-- LEFT NAV -->
<div id="leftNav">
<a href="<?=base_url();?>Research/Articles" class="selected">Articles</a>
</div>
<!-- /LEFT NAV -->
<!-- MAIN COL -->
<div id="mainColWide">
<h1><?= $post->post_title ?></h1>
<p class="details"><?= the_time('F j, Y') ?></p>
<?= wpautop($post->post_content) ?>
<br/><br/>
<a href="<?=base_url();?>Research/Articles">back</a>
</div>
<!-- /MAIN COL -->
<br class="clearFloat" />
</div>
<!-- /CONTENT -->
Research.php (Controller):
<?php
class Research extends Base_Controller {
function Research()
{
parent::Base_Controller();
}
function Index()
{
$this->data['content'] = $this->load->view('Research/index.php', $this->data, true);
$this->load->view('layouts/master' ,$this->data);
}
function Articles($type = '', $id = '')
{
$this->data['type'] = $type;
$this->data['id'] = $id;
$this->data['content'] = $this->load->view('Research/articles.php', $this->data, true);
$this->load->view('layouts/master' ,$this->data);
}
function Article($id)
{
$this->data['post'] = get_post($id);
$this->data['id'] = $id;
$this->data['content'] = $this->load->view('Research/article.php', $this->data, true);
$this->load->view('layouts/master' ,$this->data);
}
}
That’s it! This would allow us to browse all the blog entries without leaving the CI site. Other helper functions can also be found in WordPress’s documentation which can assist you in integrating the design.