Using CKEditor as a plugin in your CodeIgniter applications
CKEditor is a powerfull WYSIWYG text editor licensed under the GPL, LGPL and MPL open source licenses. CKEditor can easilly be added to any web page, you will find below a simple way to integrate CKeditor to your CodeIgniter applications.
Downloading CKEditor
The first step is to download the CKEditor editor package, note that the helper have only be tested over CKEditor 3.0.2. Once done, you should consider to remove the _samples and _sources directories from the uncompressed files.
Then, place the entire ckeditor directory into a /js/ folder. You can place it anywhere but remember to set the correct path when initializing the helper.
Adding the CKEditor helper for CodeIgniter
Download and place the ckeditor_helper.php file into the CodeIgniter’s system/application/helpers folder.
This helper can, for the moment, manage all CKEditor’s available configuration options and custom styles definitions.
Creating the controller
First of all, we are going to create a controller that will set all the helper’s configuration options. You are able to set all CKEditor’s available configuration options inside the config array. We are also going to define two custom styles to replace the CKEditor’s default styles. Note that the id must match the textarea’s id in the view.
<?php class Ckeditor extends Controller { public $data = array(); public function __construct() { parent::Controller(); $this->load->helper('url'); //You should autoload this one ;) $this->load->helper('ckeditor'); //Ckeditor's configuration $this->data['ckeditor'] = array( //ID of the textarea that will be replaced 'id' => 'content', // Must match the textarea's id 'path' => 'js/ckeditor', // Path to the ckeditor folder relative to index.php //Optionnal values 'config' => array( 'toolbar' => "Full", //Using the Full toolbar 'width' => "550px", //Setting a custom width 'height' => '100px', //Setting a custom height ), //Replacing styles from the "Styles tool" 'styles' => array( //Creating a new style named "style 1" 'style 1' => array ( 'name' => 'Blue Title', 'element' => 'h2', 'styles' => array( 'color' => 'Blue', 'font-weight' => 'bold' ) ), //Creating a new style named "style 2" 'style 2' => array ( 'name' => 'Red Title', 'element' => 'h2', 'styles' => array( 'color' => 'Red', 'font-weight' => 'bold', 'text-decoration' => 'underline' ) ) ) ); } public function index() { $this->load->view('ckeditor', $this->data); } } |
Creating the view
The ckeditor.php view only has to display a textarea element with the matched id and call the display_ckeditor() helper’s function.
<textarea name="content" id="content" ><p>Example data</p></textarea> <?php echo display_ckeditor($ckeditor); ?> |
That’s all ! If you’ve followed all the steps correctly, CKEditor should shows up in the view. Please note that I assume that you are loading also a correct header and footer view with all the xHTML required stuff.
Downloading the tutorial
Source files of this tutorial (controller, helper, and view) can be downloaded here.
Changelog
- 2010-01-12: All the stuff moved out of system/plugins.
- 2010-01-30: Fixed Internet Explorer compatibility issue.
Troubleshooting
- If you are using the .htaccess file given by the CodeIgniter’s user guide and have placed the ckeditor’s folder into system/plugins, be sure to allow the directory system to be called via HTTP in order to allow access to the plugins directory
Licence

CKEditor plugin for CodeIgniter by Samuel Sanchez is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
Based on a work at codeigniter.com.
Permissions beyond the scope of this license may be available at http://www.kromack.com/contact-kromack-developpeur-france/.
Please note that this helper is based on an original idea discussed in this CodeIgniter thread.


27 Commentaires
I have tried your tutorial, but it doesnt work. Should the textarea id same on both controller and view? You have mentioned in controller « contenu » and in the view « content » ?? can you please help me? thanks
It was an error, both id have to match, sorry about that !
How can I do to avoid writing the same code in each controller function?
I mean, one thing would be to autoload the ckeditor helper…
but I’d like to know how to avoid creating the $this->data['ckeditor'] array each time I want to send ckeditor to a view…
thanks! @kabeza
Hi, you should use a config file for the helper in order to build the config array. Then, put $this->data['ckeditor'] = $this->config->item(‘ckeditor_config’); in a pre_controller, or in a hook, or in your constructor
See http://codeigniter.com/user_guide/libraries/config.html for more informations. Good luck !
The controller example have been updated to put all the initialization stuff into the constructor. The helper have also been updated to work outside system/plugins. Use the ‘path’ key to locate your own ckeditor folder.
What does this path means in helper?
CKEDITOR_BASEPATH = ‘ » . base_url() . « system/plugins/ckeditor/’;
Hi Vova, thanks for the bug report, this was an old line from the previous release. This CKEditor’s var is now correctly setted with the dynamic path.
Hi, first thank you for the helper! It works great, except for one small thing, I don’t know if anyone else had this problem.
IE6 and IE7 (IE8 doesn’t bother) throws an error on the page, because of the last comma at the config values part (and I assume also at the styles values, which I haven’t used yet, but it’s basically the same syntax).
So I modified the helper (at the Adding config values part) and checked for the last element of the config values array, not to put the extra comma at the end.
Thanks to RegularJohn for the bug report. The files have been updated to work with Internet Explorer. More stuff for this helper is coming
Ok, I understand how I am suppose to display the editor, but how am I suppose to show the output?
Can I add class from css to styles, and how?
Tanks
Hi Johny, I think CKEditor don’t support the possibility to add CSS classes to styles. You might find a way for doing that in the docs : http://docs.cksource.com/CKEditor_3.x/Developers_Guide
Hi Samuel,
Thanks for the awesome helper. I added a function ‘get_ck_config’ to the helper to make the config easier to set, so on the view we can just call
makes it easier to have multiple textareas in one view.
Thanks again.
.tre.
Thanks very much for posting this. I followed your steps and was able to get ckeditor working in my CodeIgniter app within about 20 minutes, including customization. Very helpful!
Excellent Guide Sanchez Muchos Gracias Senor! I have got working in ten min, using smarty php templating too on front end. Thanks for your help…very much appreciated.
Thanks
Hi. Thanks for the tutorial, it was very helpfull. Can i set this, to multiple textareas in the same view? How?
Thanks again,
Ricardo
Thanks for the tutorial, it works fine! But do you have any idea how to use the editor as a form input field. I’m using the CI form helper and would like to replace some existing input fields with the editor, until now without succes. Any hint very much appreciated!
Hi Glarry, the editor is inserted in a textarea that the value can be accessed via $this->input->post(‘name_of_the_editor »), you can also add your form validation rules like you would normally do with a textarea
Forget about previous message. Replacing the form_input() with textarea was doing the trick. Thanks again!
Hi, i’m just trying your helper and i have a little side note about it:
This helper do not provide support for extra config parameters, like custom toolbar.
So i’ve added a function to retrieve toolbar data.
this is how i’ve made modifications: http://pastie.org/977809
Dummy example, in the controller file you provided, you can pass toolbar infos like this:
http://pastie.org/977816
Hope it helps
Thanks for the tutorial……but one problem,i want to add 2 ckeditors in the same page…how can i do that…..
@Muhammad Nauman Using $this->data['ckeditor_1'], $this->data['ckeditor_2'] with two different IDS doesn’t work ?
Yes Kromack i have used $this->data['ckeditor_2'] , $this->data['ckeditor_1'] from my controoler for two different editors on the view page…..but it does’nt work….
it display only first ckeditor not second……
when i apply bold property to the text of first editor it does’nt appy on first but on second editor……while the second editor work fine……what about first editor of the page….i have not found any solution yet….
@Muhammad Could you send me by the contact form an URL to your web page that I can debug ?
Hi!,
I was just about to post the same problem as Muhammad described. When I added 2 editors on the same page, only the second one worked normally. After searching for the source of the problem I found out that when I added the second editor, it added a second javascript path to ckeditor.js which seemed to « crash » the first one. So I needed a little bit of tweaking to only add once the ckeditor.js. Now it works great. Hope it helps someone.
New comment :