Make Codeigniter and WordPress Play Well Together
So, While waiting for the much anticipate Expression Engine, we had to have a non-custom solution to a client friendly CMS. It turns out its actually really easy to make wordpress and codeigniter play in the same sandbox together – with almost no configuration.
Basically what you do is install wordpress and get it running on its own, then dump in your ci setup, replacing the wordpress index file with your codeigniter one.
Next, modify the index.php (Codeigniter bootstrapper) and require in the wp-load.php file (wordpresses bootstrapper)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* |-------------------------------------- | LOAD THE FRONT CONTROLLER |-------------------------------------- | | And away we go... | */ require_once 'wp-load.php'; require_once BASEPATH.'codeigniter/CodeIgniter'.EXT; /* End of file index.php */ /* Location: ./index.php */ |
This is pretty much it.. the only other thing you need to do is to use the wp_head() to pull in all the css/js that are required from any modules.
Because all of wordpress is run from the global space with functions (as apposed to a main class like CI) all the functions are available to you in your templates and views in ci..
Now, you have a wp-admin with all your content and your client access powering content for a 100% CI front end.
What we are doing (thanks Easily Amused for the idea) is building categories for each area (like a weblog in EE) and letting our customer add posts and add them to that category to go on that page… Like a homepage cateory, the latest post gets pulled in
The following code is in my view! (sorry about the crappy formatting, this site is wordpress and it freaks out when putting wp code into posts…
—
Homepage Only Feed
1 2 3 4 5 6 7 8 9 | query_posts('numberposts=1&cat=3');
while ( have_posts() ) : the_post()
<h3><?php the_title() ?></h3>
<p><?php the_content(); ?></p>
<p>Updated <?php the_time('l, F jS, Y') ?></p>
<?php comments_template(); // Get wp-comments.php template ?>
<?php endwhile; ?>
<h3>Flickr Feed Plugin</h3>
<?php widget_flickrRSS(array()); ?> |
There is a lot more to be done, next step i will be working on is a CI library that uses the WP users/auth, so it can be portable, as well as some kind of module in WP that has easy access to CI Models & Controllers like doing an AJAX call or AMF Call


Great info!
I cannot wait until the there is a CI library that uses the WP users/auth.
-Brad
Hi i want to know how to live update in site by using codeigniter in php
@kumar what do you mean by live update??
Cool!
Any new developments on this? I’m trying to be able to control WordPress MU with a CI admin, and what I’ve got works as long as I’m logged into wordpress, if I’m not it doesn’t work…
Please feel free to contact me via email.
-Brad
Thanks for this article! I’m on my way to integrating WP with my CI install.
A question though, I heavily use site_url() in CodeIgniter, do you? how do you deal with the conflict from WP’s site_url() function?
thanks,
i dont use site_url in either.. however, i think that CI’s will be superseded by WP’s because i’m loading the WP core/functions before calling the CI core and CI does a check before it declares any new global function… either way, they should equate to the same thing.
Unfortunately you’re right- WP’s site_url() function got priority. and it’s down right frustrating.
I’m trying to figure out a way to get CI’s site_url to work.
:O
thanks,
mainly my gripe is that wp’s site_url doesn’t add the trailing slash and CI’s does. I’ll just have to go through my code and fix those slashes then.
unless you know of a better way to do it.
thanks,
How do you stop CodeIgniter from destroying the $wp_query object that WordPress creates?
I can’t seem to access it within my controllers but is ok in index.php.
hope you can help.
thanks,
Sorry, don\’t mean to \"spam\" the comments but I figured out what was wrong with my code.
it wasn\’t that codeigniter destroyed the $wp_query object but that to access it, I had to add
global $wp_query
to my code. in case someone, sometime encounters the same problem. rookie mistake on scope
Hello Phil, Thank you for your post, I got some idea how CI work with WP.
Since the way you wrote is CI power up the website, not WP.
Basically the advantage just have a ready WP backend, and can directly use some of the WP function.
the page is generate from $this->load->view , correct me if I am wrong.
My goal is actually use WP to power up the website, inside the themes folder, I can use CI to power up the theme. is that possible?
Hi,
Looks like i am searching for i am going to try it out, but does it matter that i have WP installed in a subfolder???
I havn’t tried in a sub folder, you may need to override your auto-links etc to be relative to your CI install
oh and how do you make sure the seo url are correct? a quick test and i can\’t see the preview of wp posts???
Sorry about commenting 3 times but this one explains it a bit better (i hope)
I have a CI install and WP in a subfolder. I can pull the posts in my CI views.
I am having a bit of problem with the link to a wp-post. it doesn\’t work.
Even the default from wp-admin and preview post ?p=123 doesn\’t work.
As i have installed WP in a subfolder i also tried /blog/?p=123
I also disabled .htaccess
I the end i would be happy if all wp-posts are like reachable by /blog/seo-title
and parsed with a CI view
Any tips, suggestions??
regards
HI alex,
in your loop in CI view you should be able to call the_permalink(); – this will auto generate the post link for you.. the problem is, you need to either have a module/controller structure setup in CI to handle those requests, or get creative with your CI routes and push all ^/blog to a wp controller that handles parsing the request.
Hi phil,
I encountered a major problem in my setup ( didn’t look at until now),
My dx_auth doesn’t work anymore and i think it has to do with sessions. When i disable the WP intergration it works but when i enable WP. the logged_in isn’t working anymore
i guess this returns false
return $this->ci->session->userdata(‘DX_logged_in’);
do you know how to solve this session problem???
I successfully integrated this, but i have a problem when click on categories or archives link, the page redirected to http://www.exaple.com/blog?cat=1. then my codeignitor shows the pag not found error message, any one can help me that how to solve the link problem??thanks in advance
in your WP Admin, make sure your Permalinks are setup to use the URI structure and not get vars.