How to Integrate Wordpress Template with 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 can
assist you in integrating the design.

CodeIgniter + Wordpress integration

I used the following.

My goal is to use CodeIgniter for some pages, and leave the other pages in Wordpress untouched. I only have a few steps to do it :

Copy CI folder at the Wordpress root

Modify the « index.php » of CI to add an include which will add WP functions to CodeIgniter :

@require '../wp-load.php';
require_once BASEPATH.'core/CodeIgniter.php';

Once this line added, Wordpress functions will be usable in CodeIgniter ; they will be used mainly for views.

Modify the .htaccess of WP to not rewrite CI's URLs :
After this line :

 RewriteRule ^index\.php$ - [L]

Add this line :

RewriteCond %{REQUEST_URI} !^/(codeigniter_folder|codeigniter_folder/.*)$

Then CI views can use WP functions.

How to combine / integrate CodeIgniter and Wordpress blogs functionality?

You do this you will need to create 2 files and modify 2 existing functions. One function is in CodeIgniter and the other is in Wordpress.

Here are the steps.

1.) Open your configs/hooks.php file and create a pre_controller hook as follows:

$hook['pre_controller'] = array(
'class' => '',
'function' => 'wp_init',
'filename' => 'wordpress_helper.php',
'filepath' => 'helpers'
);

2.) Create a new file in your helpers directory called 'wordpress_helper.php', and add the following code to it:


/**
*
*/
function wp_init(){

$CI =& get_instance();

$do_blog = TRUE; // this can be a function call to determine whether to load CI or WP

/* here we check whether to do the blog and also we make sure this is a
front-end index call so it does not interfere with other CI modules.
*/
if($do_blog
&& ($CI->router->class == "index" && $CI->router->method == "index")
)
{

// these Wordpress variables need to be globalized because this is a function here eh!
global $post, $q_config, $wp;
global $wp_rewrite, $wp_query, $wp_the_query;
global $allowedentitynames;
global $qs_openssl_functions_used; // this one is needed for qtranslate

// this can be used to help run CI code within Wordpress.
define("CIWORDPRESSED", TRUE);

require_once './wp-load.php';

define('WP_USE_THEMES', true);

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

// were done. No need to load any more CI stuff.
die();

}

}

3.) Open wp-includes/link-template.php and made the following edit:

if ( ! function_exists('site_url'))
{
function site_url( $path = '', $scheme = null ) {
return get_site_url( null, $path, $scheme );
}

}

4.) Copy url_helper.php from the CodeIgniter helper folder to your APPPATH helper folder
and make the following edit:

if ( ! function_exists('site_url'))
{
function site_url($uri = '', $scheme = null)
{

// if we are in wordpress mode, do the wordpress thingy
if(defined('CIWORDPRESSED') && CIWORDPRESSED){

return get_site_url( null, $path, $scheme );

}else{

$CI =& get_instance();
return $CI->config->site_url($uri);
}
}

}

The steps above will allow you to dynamically load either your CI app or your WP site based on some simple filtering. It also gives you access to all CI functionality within WP of that is something you can use.

Codeigniter view in Wordpress

You directly cannot call CI view in wordpress file. Inorder to make changes to wordpress header and footer you will need to modify header.php and footer.php file under wordpress theme (wp-content/themes/your_theme_folder).
Also make sure that you do not modify wordpress default theme directly as any changes to default theme would be lost during wordpress updates.
Best practice is to create child theme and then make your changes over there
https://codex.wordpress.org/Child_Themes , here the guide to create child theme.

How to use Codeigniter in existing wordpress site?

/website/
/ci/ (codeigniter runs inside here)
/wp-content/
/wp...
...
index.php (wordpress)
.htaccess (combined wordpress / ci functionality)

Make suru you configure the Codeigniter correctly. Take a look at this guide.

Also following questions

  • How Can I integrate Wordpress with CodeIgniter?
  • How to integrate WordPress template with CodeIgniter

Integrating a codeigniter site as a wordpress plugin

The issue is that I am trying to run in an iFrame rather than propperly in WP with the short code. I fixed the issues with the shortcode with advice given here:
How to integrate WordPress template with CodeIgniter.

Now all works fine.

Use wordpress 3.x.x functions in an external application

Scratch that, its obviously the wrong approach to making the two systems work together but if you're in a situation like me where your code igniter website is long established and so is the wordpress set up then find another route. If you have wordpress setup and are about to start a code igniter app the consider how they'll integrate before getting into it. Perhaps write a wordpress plugin instead, if its the otherway around then consider writing your own blog application with codeigniter.

In my case I have no choice with two established systems. Wordpress provides an XML-RPC interface. I've decided it will be a lot less painful to use it. See http://codex.wordpress.org/XML-RPC_Support for more info on it. http://wordpress.org/extend/plugins/extended-xml-rpc-api/

Might also be useful to someone:
http://wordpress.org/extend/plugins/wp-restful/



Related Topics



Leave a reply



Submit