Adding CKEditor to webpages in PHP #Code

What is CKEditor

CKEditor is an open source, customizable web text editor that can be integrated to our webpages. It can be used in three different modes (Article editor, Document editor and Inline editor) for content creation. I was looking for a web editor like Google doc using which I can collect text data from students (but not requiring login with gmail), and I found CKEditor doing exactly what I wanted to do. I’m using it here as a web document editor.

In this blog post, I’m combining a few steps I did to integrate CKEditor to my webpage. This is the code I wrote after a few rounds of trial and error and many rounds of looking up on the CKEditor documentation and StackOverflow. Wish I found a blog like this when I was trying to implement this 😉

Setting up CKEditor

CKEditor is available for download here. I used the Standard package of the current stable version  (Version 4.6.2 • 12 Jan 2017). All you have to do is to copy the folder ‘ckeditor’ from the downloaded zip file to your program files folder and you’re ready to go. The main steps are below:

Include ckeditor.js in the head section of your code:

Create a text area for the editor in the body section followed by your CKEditor instance:

This simple full code renders you a CKEditor with the default configuration options (Default toolbar, width, height etc. – All of these can be customized).

Including a default text in CKEditor

To include a default text in the editor, just replace the text within your textarea with your desired text. Include html tags for formatting if needed. Full code below:

Resizing CKEditor and editing its toolbar

To change the default height and width of the CKEditor, modify your code as below:

If you also want to customise your toolbar, you can do so as below:

This code will give you a customized editor with only the tools you selected like this:

Alternatively, if you want to keep most of the tools, but only remove a few buttons, you can update the config.js file of your CKEditor to include this line:

The complete list of buttons is here.

My configured CKEditor now looks like this:

Track changes in text

CKEditor has a plugin called LoopIndex Track Changes to track the changes made to the text inside the editor. This is similar to the Review -> Track changes option in Microsoft Word, which when enabled tracks all changes made to the text. This works like a charm in three easy steps:

1. Download the plugin and copy the ‘lite’ folder to the plugins folder of CKEditor.

2. Add lite-interface.js to your script along with the prior ckeditor.js

3. In config.js of the CKEditor, include the following code:

Now, the editor can track and highlight the changes made to the text:

Saving text from a CKEditor

There are a few ways to save data from the editor. I’m using a post method to get the data from the text area and store it in my mySQL database. This is my submit.php file where I get the data from the editor and store it in my database after cleaning:

My final code for the editor looks like this:

I’m also working to see if I can track key strokes or take snapshots of the drafting process. Will write another post when that succeeds 🙂

2 thoughts on “Adding CKEditor to webpages in PHP #Code”

  1. HI,
    I got this part very easily, but I’m wondering if you can help me with this little problem.
    If I put my code in a single file to get a single webpage I get the results. When I introduce it to the main page via get data from another file with a function type like ajax it’s not working, tried multiple solution but no winning one. I’ve put the script tag with the url for ckeditor and still no luck. But if I code it like just one page for a single website page, ckeditor works fine but not when link to another coded page in 2 separate files that gives one website page

    1. I haven’t seen your code, but you CAN get data from another page to load in ck editor. You can use the data from your AJAX call in ckEditor directly.

      Alternatively, try storing the data as a session variable ($_SESSION[‘editorText’] = yourdata), then use that text ($my_text = $_SESSION[‘editorText’] ) when initializing ckeditor like below:
      Inside the textarea for ckeditor:
      < ?php echo $my_text; ?>
      Close textarea

Leave a Reply

Your email address will not be published. Required fields are marked *