Recently 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.
Tags: codeigniter, php, wordpress
Jesal-
That is a pretty interesting concept how you did that. I am at this point on a large site built with codeigniter and was hoping to work WP in as the blog. I, as well, put the wp-blog-header.php in my CI index.php. I also extended the core url helper on the anchor() and redirect() functions since CI and WP both use site_url. I thought things were progressing well until I started modifying the WP template to look like my site. First it balked at my use of base_url() in my header for my CSS, JS, etc, Then, my use of $this->tank_auth… for the logged-in/logged out logic in some of my navigation. Finally, it hit me that although I was able to use WP template tags in my CI site…I was not going to get any CI functionality over on the WP side.
Your solution sounds interesting. This might actually be better, because I wanted to keep the blog posts in a narrow silo of categories.
Thanks for the post. I will try this out.
Thanks Darren. Yeah this is probably the easiest way of going about WP integration especially as you said if you want to keep the blog posts in a narrow silo of categories.
Although these days since the introduction of version WordPress 3.0, I prefer to build the entire site on top of WordPress, as they’ve made easier to use it as a CMS with the introduction of custom post types. If for some reason we have to stick to CI I’d consider using FuelCMS or PyroCMS
Anyway let me know how this solution works out for you!
J-
Yes, after my first comment I found that I could drop my application folders into FuelCMS and PyroCMS and go with it that way also. Like you, I have spent a considerable amount of time over the years loving WP. Plus, as you point out with 3.0 it just keeps getting better and better. This particular site of mine is very custom and db intensive so CI suits it very well in ways that WP is just not intended.
I found a a post by the FuelCMS boys how they used to integrate WP with CI prior to running full-bore with FuelCMS. http://www.thedaylightstudio.com/the-whiteboard/2010/06/16/codeigniter-and-wordpress-integration
I’m gonna give it a go since I don’t have time to spend learning new CMS prior to my re-launch deadline.
Oh that’s a good find. Thanks for sharing the link and good luck!
Can you recommend any websites on this topic for further reading?