How to integrate WordPress template with CodeIgniter

How can CodeIgniter and WordPress be integrated such that the look and feel/template of
the WordPress blog is carried over to the CodeIgniter-created pages?

Related posts

Leave a Reply

6 comments

  1. First step is to move CodeIgniter and the WordPress files in their own directory.

    After that, put the following line 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('../wp-blog-header.php');
    

    Then, you can use the following functions inside your views:

    <?php
        get_header();
        get_sidebar();
        get_footer();    
    ?>
    

    Other helper functions can also be found in WordPress’s documentation which can
    assist you in integrating the design.

  2. When I included the file wp-blog-header.php in Codeigniter’s index.php page, I got a problem that site_url() is defined in both codeigniter’s URL helper and WordPress. I solved this using the following code:

    require('blog/wp-blog-header.php');
    
    add_filter('site_url', 'ci_site_url', 1);
    
    function ci_site_url() {
        include(BASEPATH.'application/config/config.php');
        return $config['base_url'];
    }
    
    header("HTTP/1.0 200 OK");
    

    Last line needs to be added as WordPress file was adding a HTTP response header ‘HTTP/1.0 404 Page not found’ to the header.

    Now its fine to use WordPress functions to call in CodeIgntier.

  3. Here is another way to use WordPress templates in your codeigniter project. This works better for me so I wanted to share it. Tested with WordPress 3.3.1 and Codeigniter 2.1.

    Directory Structure:

    / - WordPress
    /ci/ - codeigniter
    

    /ci/index.php (Top of CI Index file)

    $wp_did_header = true;
    
    if ( defined('E_RECOVERABLE_ERROR') )
        error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR |   E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
    else
        error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);
    
    require_once("../wp-config.php");
    

    Deal with the site_url function collision by overriding the default codeigniter version. You will need to change any place you used site_url() in codeigniter to use ci_site_url() instead.

    /ci/application/helpers/MY_url_helper.php

    <?php
    function anchor($uri = '', $title = '', $attributes = '')
    {
        $title = (string) $title;
    
        if ( ! is_array($uri))
        {
            $site_url = ( ! preg_match('!^w+://! i', $uri)) ? ci_site_url($uri) : $uri;
        }
        else
        {
            $site_url = ci_site_url($uri);
        }
    
        if ($title == '')
        {
            $title = $site_url;
        }
    
        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }
    
        return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
    }
    
    
    if ( ! function_exists('ci_site_url'))
    {
        function ci_site_url($uri = '')
        {
            $CI =& get_instance();
            return $CI->config->site_url($uri);
        }
    }
    
    function current_url()
    {
        $CI =& get_instance();
        return $CI->config->ci_site_url($CI->uri->uri_string());
    }
    
    
    function anchor_popup($uri = '', $title = '', $attributes = FALSE)
    {
        $title = (string) $title;
    
        $site_url = ( ! preg_match('!^w+://! i', $uri)) ? ci_site_url($uri) : $uri;
    
        if ($title == '')
        {
            $title = $site_url;
        }
    
        if ($attributes === FALSE)
        {
            return "<a href='javascript:void(0);' onclick="window.open('".$site_url."', '_blank');">".$title."</a>";
        }
    
        if ( ! is_array($attributes))
        {
            $attributes = array();
        }
    
        foreach (array('width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes', 'screenx' => '0', 'screeny' => '0', ) as $key => $val)
        {
            $atts[$key] = ( ! isset($attributes[$key])) ? $val : $attributes[$key];
            unset($attributes[$key]);
        }
    
        if ($attributes != '')
        {
            $attributes = _parse_attributes($attributes);
        }
    
        return "<a href='javascript:void(0);' onclick="window.open('".$site_url."', '_blank', '"._parse_attributes($atts, TRUE)."');"$attributes>".$title."</a>";
    }
    
    
    
    function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = ci_site_url($uri);
        }
    
        switch($method)
        {
            case 'refresh'  : header("Refresh:0;url=".$uri);
                break;
            default         : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }
    

    You can now use the WordPress get_header() and/or get_footer() functions to draw the template in your CI project.

  4. I’m using WordPress for managing articles in a custom CI e-commerce website. CI is my main site. The directory structure is the following:

     /application (CI)
     /... (directories like javascript, stylesheets ...)
     /system (CI)
     /wordpress
     /.htaccess
     /index.php (CI)
    

    I’m able to use WordPress functions in my CI controllers without my URLs being messed up when adding the following code to the top of CI’s index.php:

    require_once './wordpress/wp-blog-header.php';
    
    add_filter('site_url', 'ci_site_url', 1);
    
    function ci_site_url($uri = '') {
        $CI =& get_instance();
        $uri = ltrim(str_replace($CI->config->base_url('wordpress/'), '', $uri),'/'); // "wordpress/" is in my case the name of the directory where I installed WordPress. See directory structure above.
        return $CI->config->site_url($uri);
    }
    

    Works also when using CI i18n library by Jérôme Jaglale (http://jeromejaglale.com/doc/php/codeigniter_i18n).

  5. if you’re planning on using the code ignitor site_url function in your code, or if you’re doing a merge of an existing CI site and WP… this might be helpful:

    at the top of CI index.php:

    require_once '../wp-blog-header.php';
    
    add_filter('site_url', 'ci_site_url', 4);
    
    function ci_site_url($url, $path, $orig_scheme, $blog_id) {
        $CI =& get_instance();
        $new_path = str_replace("YOURSITEURLGOESHERE", "", $url);
        return  $CI->config->site_url($new_path);
    }
    

    effectively this allows you to use site_url in CI, so if you’ve already added a ton of links and content to your project it might help you out.

  6. In WordPress 6.1 Version and Codeigniter – 4

     /* Folder structure like
       Public_html/ WordPress
       Public_html/Codeigniter /*
    
    
    
     //Call WordPress wp_load file inside CodeIgniter in this way 
       // Add this code into Public_html/Codeigniter/Public/index.php
    
     $mypath = $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
     include_once($mypath);
    
    
    
     // Call header and Footer in to view page 
      // Add this code into Public_html/Codeigniter/App/Views/welcome_message.php
       
    <?php get_header(); ?>
     
    <body>
        <h3>
    It works!</h3>
     
    </body>
    </html>
     
    <?php get_footer(); ?>