How to Integrate CodeIgniter with WordPress

WordPress and CodeIgniter both are the most popular framework in PHP. CodeIgniter is a lightweight PHP framework that can help in developing applications that can be integrated with WordPress.While some users prefer developing the CodeIgniter and WordPress elements separately, we will go through an example where the primary navigation and footer are similar on both platforms, because it doesn’t make much sense to update the navigation and footer twice.

Here we learn about two things using WordPress and CodeIgniter

1. How to integrate already running WordPress website to integrate CodeIgniter
2. How to integrate already running Codeigniter website to integrate WordPressWordpress And CodeIgniter

1. Integrate CodeIgniter into WordPress

It comes as a meager shock that numerous site proprietors overseers still lean toward WordPress as their essential Content Management System (CMS), and you can’t point the finger at them considering the far-reaching highlights that WordPress brings to the table.

Okay, let’s start to integrate CodeIgniter into WordPress here, WordPress is our primary application and we will use its functionality within CodeIgniter while keeping the CodeIgniter app in a sub-directory.

Step 1: Adding the WordPress Bootstrap File

WordPress’s wp-load.php file immediately preceding the call to Codeigniter bootstrap file in CI’s index.php file

Before adding any WordPress functionality to CI’ file.we will testing to make sure everything works fine.The first page of our app looked fine, but that was about it.

Every URL within our app, including form handlers, were redirected to the root directory (i.e. WordPress’ directory), resulting in 404 errors. We discovered that both WordPress and Codeigniter have a “site_url” function in global scope. By loading the WordPress bootstrap file before CodeIgniter, we were getting WordPress site_url.Codeigniter function “failed” silently because it was checking to see if a site_url function existed.

Step 2: Extending Codeigniter URL Helper

Our solution was to ‘extend’ the URL helper of CodeIgniter. By using ci_site_url, we can resolve the site_url issue. The My_url_helper.php (helper) file should be in the application directory of CodeIgniter.

Step 3: Change site_url to ci_site_url in the CodeIgniter App.

Replace all site_url file references with ci_site_url into your applications, controllers, models, and directories.

Once we completed these steps, our CI app worked …

Step 4: Stop WordPress from managing our cookies!

Our site that still was not working was the admin panel that we built in Codeigniter.our credentials have been accepted but, still redirecting to login page.

I think you know about CodeIgniter will create cookies for every active database session when you log in.
This can lead to WordPress bootstrap file rectifying the issue. This happens when WordPress loops via the $_COOKIE global and implements add_magic_quotes functionality to all elements, except the CodeIgniter cookie.

The code is as follows:

Comment out the following lines inside the wp_magic_quotes function and add a reference to the above ci_ignore_magic_quotes function.

After that, at the very top of the file, we need to tell WordPress to not upset our $_COOKIE[‘ci_session’]:

All Done!!!everything worked as it should.Now, you can start using WordPress with your CodeIgniter application.


2. Integrate WordPress into CodeIgniter

IF you have already created website in CodeIgniter and you have added blog page then WordPress is the best option so,
First of all, install WordPress in a folder called the blog on your website. It would be located in the top level directory in your CodeIgniter website. So it would be alongside the system folder of the CI installation. If you already have a DB for your site, have WordPress add tables to that same DB with a prefix to make things simple.

After, that add the following line in the 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 root directory.

Now, create a blog.php file into CodeIgniter’s VIEW folder, and write the below code:

Now, create a blog-post.php in CodeIgniter’s CONTROLLER folder, and write the below code:

All done!! This code allows to WordPress blog without leaving CodeIgniter site. More helper function explains in details in WordPress documentation.