Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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

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).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top