How to integrate WordPress template with CodeIgniter How to integrate WordPress template with CodeIgniter codeigniter codeigniter

How to integrate WordPress template with CodeIgniter


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 canassist you in integrating the design.


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.


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

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