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)


/*
|--------------------------------------
| 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


    query_posts('numberposts=1&cat=3');
    while ( have_posts() ) : the_post()
     

Updated

Flickr Feed Plugin

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

Be Sociable, Share!

39 Comments

  • July 1, 2009 - 3:56 pm | Permalink

    Great info!

    I cannot wait until the there is a CI library that uses the WP users/auth.

    -Brad

    • kumar
      September 8, 2009 - 2:39 am | Permalink

      Hi i want to know how to live update in site by using codeigniter in php

      • September 9, 2009 - 5:29 am | Permalink

        @kumar what do you mean by live update??

  • July 6, 2009 - 10:04 am | Permalink

    Cool!

  • August 19, 2009 - 3:27 am | Permalink

    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

  • December 9, 2009 - 12:40 am | Permalink

    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,

    • December 9, 2009 - 12:47 am | Permalink

      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.

      • December 9, 2009 - 4:50 am | Permalink

        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,

      • December 9, 2009 - 5:06 am | Permalink

        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,

      • Jared
        April 4, 2011 - 3:49 pm | Permalink

        This has been bugging me as well, but I found that if you get a new instance :

        $CI =& get_instance();

        you can use $CI->config->site_url() instead

    • December 31, 2010 - 12:42 pm | Permalink

      Try changing your code in index.php to this:

      require(‘./featured/wp-load.php’);

      add_filter(‘site_url’, ‘ci_site_url’, 1);
      function ci_site_url()
      {
      include(APPPATH.’/config/config.php’);
      return $config['base_url'];
      }

      • December 31, 2010 - 12:56 pm | Permalink

        Please edit the code in my comment to the following (I didnt’ look it over well enough ebfore posting)

        require(‘wp-load.php’);

        add_filter(‘site_url’, ‘ci_site_url’, 1);
        function ci_site_url()
        {
        require_once(APPPATH.’/config/config.php’);
        return $config['base_url'];
        }

  • December 10, 2009 - 12:27 am | Permalink

    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,

    • December 10, 2009 - 12:57 am | Permalink

      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

  • Shiro
    December 17, 2009 - 2:25 am | Permalink

    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?

  • alex
    February 22, 2010 - 5:04 pm | Permalink

    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???

    • February 23, 2010 - 12:22 pm | Permalink

      I havn’t tried in a sub folder, you may need to override your auto-links etc to be relative to your CI install

  • alex
    February 22, 2010 - 5:52 pm | Permalink

    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???

  • alex
    February 22, 2010 - 6:09 pm | Permalink

    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

    • February 23, 2010 - 12:22 pm | Permalink

      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.

  • alex
    February 26, 2010 - 2:30 am | Permalink

    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???

    • June 28, 2010 - 4:14 am | Permalink

      Try enabling database sessions. WordPress removes native PHP $_SESSION handling, however if in CodeIgniter’s application/config/config.php you set:
      $config['sess_use_database'] = TRUE;
      it should work.

  • nithya
    April 19, 2010 - 11:24 pm | Permalink

    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

    • April 20, 2010 - 10:25 am | Permalink

      in your WP Admin, make sure your Permalinks are setup to use the URI structure and not get vars.

  • July 11, 2010 - 12:11 pm | Permalink

    My freelancer site http://www.ofreelance.com is built using Codeigniter. What advantages will it give me by integrating with WordPress?

    Codeigniter front end and wordpress backend? thats it?

    Is anything a miss on codeigniter backend?

  • December 11, 2010 - 9:49 am | Permalink

    Sounds very interesting. I’ve been looking for a way to build a site with more than 300 pages with WordPress.

    I’ve played with Custom Post Types and Taxonomies, but I get the feeling that WordPress still isn’t quite ready for prime time for a site that big.

    The other problem I have is trying to figure out a way to add more structured info to some pages. Jumping through hoops to get this to work with Custom Post Types and Taxonomies seems like more trouble than it’s worth.

    Would love to see a follow-up post with more details about your experience!

    Kurt

    • December 11, 2010 - 10:03 am | Permalink

      Hi Kurt, I’ve pretty much given up on trying to merge the two.. not because its ‘hard’ but with WordPress 3 and the new custom post types, it does everything i need it to – you just need to be creative. The only issu with custom post types at the moment is that there is not a default index for the type.. like .com/books doesn’t automatically index all your book types (but there are many posted solutions, and WP guys have it tagged for the next WP upgrade). Any ‘programming’ needed to be done can pretty easily be put into a plugin / widget and you can use short tags for a lot.. Take a look at this book ( a lot of review, but it goes from ground up on everything wordpress ) http://digwp.com/book/

  • December 11, 2010 - 10:37 am | Permalink

    Thanks Phil,

    I guess I just need to soldier through with Custom Post Types and see if I can make them do what I need. I hadn’t thought of creating a plugin to cover some of my needs – I’ll have to investigate that option in more detail.

    I appreciate the link to the book – I’ll definitely check it out.

    Cheers!

  • brent
    May 1, 2011 - 9:28 am | Permalink

    I went through the steps above and everything worked great!.. however, when I put in my header it shows the page links, I click on the page links and I get a 404 error.

    I followed you advice on changing the permalinks to use the uri structure of /%category%/%postname% but that didn’t help. Is there anything else I might need to do to make that work correctly?

  • January 10, 2012 - 1:53 pm | Permalink

    this great I was so much confused about this codeignater and wordpress, concept, but now i understood bit will try this techniques,,,thanks for share

  • diyafury
    January 11, 2012 - 6:40 am | Permalink

    Hi guys

    I’ve just integrated WordPress with an existing CodeIgnitor webapp successfully (i hope – until the next issue crops up). The idea being that one of my codeIgnitor sections will embed some blog elements…hence I will be creating a custom wp front-end within codeIgnitor but using the wp back-end.

    My CodeIgnitor site_url method as well as its sessions are working and I’ve managed to pull out a blog post.

    Here’s my steps:
    1. install wordpress in a blog directory at the root of the codeIgnitor application.
    2. add the following to my codeIgnitor index.php:

    date_default_timezone_set(‘Europe/London’);

    // INCLUDE THIS FOR WP
    require_once ‘blog/wp-load.php’; // to allow for integration of wordpress elements in codeIgniter templates

    // handle wp site_url overwriting ci site_url by stripping out the ‘/blog’
    add_filter(‘site_url’, ci_site_url, 1);
    function ci_site_url($url = “”) {
    if ($url) {
    $url = preg_replace(‘/\/blog?/’, ”, $url);
    }
    return $url;
    }
    // END INCLUDE THIS FOR WP

    require_once BASEPATH.’codeigniter/CodeIgniter’.EXT;

    3. In codeIgnitor application/config/config.php, enable the following variable:

    $config['sess_encrypt_cookie'] = TRUE; // enabled to fix session when running codeIgnitor with WordPress

    4. In wordpress admin:
    My wordpress url is set to http://mysite/blog
    My site url is currently set to http://mysite/blog, but I will change this to be http://mysite/my_section_that_includes_wp_elements to fix the permalinks.

    That’s it – so far so good.

    Hope this helps

  • diyafury
    January 11, 2012 - 6:40 am | Permalink

    Hi guys

    I\’ve just integrated WordPress with an existing CodeIgnitor webapp successfully (i hope – until the next issue crops up). The idea being that one of my codeIgnitor sections will embed some blog elements…hence I will be creating a custom wp front-end within codeIgnitor but using the wp back-end.

    My CodeIgnitor site_url method as well as its sessions are working and I\’ve managed to pull out a blog post.

    Here\’s my steps:
    1. install wordpress in a blog directory at the root of the codeIgnitor application.
    2. add the following to my codeIgnitor index.php:

    date_default_timezone_set(\’Europe/London\’);

    // INCLUDE THIS FOR WP
    require_once \’blog/wp-load.php\’; // to allow for integration of wordpress elements in codeIgniter templates

    // handle wp site_url overwriting ci site_url by stripping out the \’/blog\’
    add_filter(\’site_url\’, ci_site_url, 1);
    function ci_site_url($url = \"\") {
    if ($url) {
    $url = preg_replace(\’/\\/blog?/\’, \’\', $url);
    }
    return $url;
    }
    // END INCLUDE THIS FOR WP

    require_once BASEPATH.\’codeigniter/CodeIgniter\’.EXT;

    3. In codeIgnitor application/config/config.php, enable the following variable:

    $config[\'sess_encrypt_cookie\'] = TRUE; // enabled to fix session when running codeIgnitor with WordPress

    4. In wordpress admin:
    My wordpress url is set to http://mysite/blog
    My site url is currently set to http://mysite/blog, but I will change this to be http://mysite/my_section_that_includes_wp_elements to fix the permalinks.

    That\’s it – so far so good.

    Hope this helps

  • diyafury
    January 11, 2012 - 6:42 am | Permalink

    PLEASE NOTE THAT IN MY COMMENT ABOVE – all the single quotes have been escaped with a backslash, so please remove these escape characters if you are going to give my code a go.

  • March 10, 2012 - 9:25 am | Permalink

    This was very helpful. I found a seemingly elegant way of doing it that is working well for me so far. It requires 3 small steps.

    1.) In the front controller (/index.php) I only added this line:

    require_once ‘wp-load.php’;

    2.) In application/config/routes.php I changed the 404 route:

    $route['404_override'] = ‘index/loadwordpress’;

    3.) In /application/controllers/index.php I added this function:

    /**
    * This is a wordpress hack.
    *
    */
    public function loadwordpress(){

    define(‘WP_USE_THEMES’, true);

    /** Loads the WordPress Environment and Template */
    require(‘./wp-blog-header.php’);

    }

  • May 4, 2012 - 4:55 pm | Permalink

    Hi,

    I rly love both CI and WordPress but I was wondering the following thing:

    When using CI as frontend and WP as backend, are you still using the themes folder inside WP and how? Or are you just generating every layout using CI views ?

    Are there any examples available to have a look at or so ?

    Thanks

    • May 4, 2012 - 4:58 pm | Permalink

      Both, im using my own code in CI but then calling the sidebars / header & footer wp calls.. this way it renders the proper theme, but CI code is calling the functions

  • May 5, 2012 - 5:04 am | Permalink

    So what I am doing here is what you are doing aswell?

    This is my CI header.php which calls the WP template:

    That way I can use my header.php view anywhere inside my CI application…

    Or do u directly call the header and footer WP calls in your other views?

  • May 5, 2012 - 5:06 am | Permalink

    Ok my code had been removed in previous post, but my CI header.php view will only contain one line of php code: a call to the WP get_header function.

    This will call the WordPress header, this way my CI application will be able to handle template changes in admin panel.

    But I feel this way I am not using the power of CI views for my header and footer.
    I would like to implement a template engine later on, so my header and footer will not be able to use these template tags this way…

  • enchance
    June 18, 2012 - 2:54 pm | Permalink

    Perfect! I tweaked a few more things to make it function even better but overall this works.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    *

    * Copy This Password *

    * Type Or Paste Password Here *

    7,555 Spam Comments Blocked so far by Spam Free Wordpress

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>