Dear Reader, let us know that, “How to integrate CKEditor in Codeigniter with Live Demo“. So in this Tutorial, We Learn Step By Step “CKEditor Integration in Codeigniter”.
CKEditor (formerly known as FCKeditor) is a rich text editor which enables writing content directly inside of web pages or online applications
First, we see Snapshot for CKEditor integration form
And you can also see Live Demo by click on bellow Demo button
Step 1:- First of all we need to download ckeditor from here
Step 2:- Now we need to download ckfinder form here
Step 3:- Now we go to our project root directory like “C:\xampp\htdocs\ci_demo” and create assets folder in which paste downloaded files and extract “cdeditor.zip” and “ckfinder.zip” files
Step 4:- Now go to your project’s controller folder and create ckeditor folder in which create “ckeditorController.php” controller like “htdocs\ci_demo\application\controllers\ckeditor\ckeditorController.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?php class CkeditorController extends CI_Controller { // extends CI_Controller for CI 2.x users public $data = array(); public function __construct() { //parent::Controller(); parent::__construct(); $this->load->helper('ckeditor_helper'); //Ckeditor's configuration $this->data['ckeditor'] = array( //ID of the textarea that will be replaced 'id' => 'content', 'path' => 'js/ckeditor', //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' ) ) ) ); $this->data['ckeditor_2'] = array( //ID of the textarea that will be replaced 'id' => 'content_2', 'path' => 'js/ckeditor', //Optionnal values 'config' => array( 'width' => "100%", //Setting a custom width 'height' => '100px', //Setting a custom height 'toolbar' => array( //Setting a custom toolbar array('Bold', 'Italic'), array('Underline', 'Strike', 'FontSize'), array('Smiley'), '/' ) ), //Replacing styles from the "Styles tool" 'styles' => array( //Creating a new style named "style 1" 'style 3' => array ( 'name' => 'Green Title', 'element' => 'h3', 'styles' => array( 'color' => 'Green', 'font-weight' => 'bold' ) ) ) ); } public function index() { $this->load->library('CKEditor'); $this->load->library('CKFinder'); //Add Ckfinder to Ckeditor $this->ckfinder->SetupCKEditor($this->ckeditor,'../../assets/ckfinder/'); $this->load->view('ckeditor/ckeditor', $this->data); } } |
Step 5:- Now go to your project’s view folder and create ckeditor folder in which create “ckeditor.php” view file like “htdocs\ci_demo\application\views\ckeditor\ckeditor.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!DOCTYPE html> <html lang="en"> <head> <title>How to Integrate CKeditor in Codeigniter using Bootstrap</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script type="text/javascript" src="<?php echo base_url(); ?>assets/ckeditor/ckeditor.js"></script> <script type="text/javascript" src="<?php echo base_url(); ?>assets/ckfinder/ckfinder.js"></script> </head> <body> <div class="container"> <h1 class="alert alert-info">How to Integrate CKeditor in Codeigniter using Bootstrap</h1> <form method="post"> <div class="row"> <div class="col-sm-3 col-lg-12"> <div class="form-group"> <label>Title:</label> <input type="text" name="title" placeholder="Title" class="form-control"> </div> </div> </div> <div class="row"> <div class="col-sm-3 col-lg-12"> <div class="form-group"> <label>Description: </label> <?php echo $this->ckeditor->editor("textarea name","default textarea value"); ?> </div> </div> </div> <div class="row"> <div class="col-sm-3 col-lg-12"> <div class="form-group"> <a href="<?php echo base_url(); ?>posts"><button class="btn btn-warning">Back</button> <input type="submit" name="submitBtn" value="Add" class="btn btn-success"> </div> </div> </div> </form> </div> </body> </html> |
Step 6:- Now we need to go to our project’s library folder and create “CKEditor.php” library file like “htdocs\ci_demo\application\libraries\CKEditor.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
<?php class CKEditor { /** * The version of %CKEditor. */ const version = '3.6.1'; /** * A constant string unique for each release of %CKEditor. */ const timestamp = 'B5GJ5GG'; public $basePath; public $config = array(); public $initialized = false; public $returnOutput = false; public $textareaAttributes = array( "rows" => 8, "cols" => 60 ); public $timestamp = "B5GJ5GG"; /** * An array that holds event listeners. */ private $events = array(); /** * An array that holds global event listeners. */ private $globalEvents = array(); function __construct($basePath = null) { if (!empty($basePath)) { $this->basePath = $basePath; } } public function editor($name, $id, $value = "", $config = array(), $events = array()) { $attr = ""; foreach ($this->textareaAttributes as $key => $val) { $attr.= " " . $key . '="' . str_replace('"', '"', $val) . '"'; } $out = "<textarea name=\"" . $name . "\" id=\"" . $id . "\"" . $attr . ">" . htmlspecialchars($value) . "</textarea>\n"; if (!$this->initialized) { $out .= $this->init(); } $_config = $this->configSettings($config, $events); $js = $this->returnGlobalEvents(); if (!empty($_config)) $js .= "CKEDITOR.replace('".$name."', ".$this->jsEncode($_config).");"; else $js .= "CKEDITOR.replace('".$name."');"; $out .= $this->script($js); if (!$this->returnOutput) { print $out; $out = ""; } return $out; } public function replace($id, $config = array(), $events = array()) { $out = ""; if (!$this->initialized) { $out .= $this->init(); } $_config = $this->configSettings($config, $events); $js = $this->returnGlobalEvents(); if (!empty($_config)) { $js .= "CKEDITOR.replace('".$id."', ".$this->jsEncode($_config).");"; } else { $js .= "CKEDITOR.replace('".$id."');"; } $out .= $this->script($js); if (!$this->returnOutput) { print $out; $out = ""; } return $out; } public function replaceAll($className = null) { $out = ""; if (!$this->initialized) { $out .= $this->init(); } $_config = $this->configSettings(); $js = $this->returnGlobalEvents(); if (empty($_config)) { if (empty($className)) { $js .= "CKEDITOR.replaceAll();"; } else { $js .= "CKEDITOR.replaceAll('".$className."');"; } } else { $classDetection = ""; $js .= "CKEDITOR.replaceAll( function(textarea, config) {\n"; if (!empty($className)) { $js .= " var classRegex = new RegExp('(?:^| )' + '". $className ."' + '(?:$| )');\n"; $js .= " if (!classRegex.test(textarea.className))\n"; $js .= " return false;\n"; } $js .= " CKEDITOR.tools.extend(config, ". $this->jsEncode($_config) .", true);"; $js .= "} );"; } $out .= $this->script($js); if (!$this->returnOutput) { print $out; $out = ""; } return $out; } public function addEventHandler($event, $javascriptCode) { if (!isset($this->events[$event])) { $this->events[$event] = array(); } // Avoid duplicates. if (!in_array($javascriptCode, $this->events[$event])) { $this->events[$event][] = $javascriptCode; } } public function clearEventHandlers($event = null) { if (!empty($event)) { $this->events[$event] = array(); } else { $this->events = array(); } } public function addGlobalEventHandler($event, $javascriptCode) { if (!isset($this->globalEvents[$event])) { $this->globalEvents[$event] = array(); } // Avoid duplicates. if (!in_array($javascriptCode, $this->globalEvents[$event])) { $this->globalEvents[$event][] = $javascriptCode; } } public function clearGlobalEventHandlers($event = null) { if (!empty($event)) { $this->globalEvents[$event] = array(); } else { $this->globalEvents = array(); } } private function script($js) { $out = "<script type=\"text/javascript\">"; $out .= "//<![CDATA[\n"; $out .= $js; $out .= "\n//]]>"; $out .= "</script>\n"; return $out; } private function configSettings($config = array(), $events = array()) { $_config = $this->config; $_events = $this->events; if (is_array($config) && !empty($config)) { $_config = array_merge($_config, $config); } if (is_array($events) && !empty($events)) { foreach ($events as $eventName => $code) { if (!isset($_events[$eventName])) { $_events[$eventName] = array(); } if (!in_array($code, $_events[$eventName])) { $_events[$eventName][] = $code; } } } if (!empty($_events)) { foreach($_events as $eventName => $handlers) { if (empty($handlers)) { continue; } else if (count($handlers) == 1) { $_config['on'][$eventName] = '@@'.$handlers[0]; } else { $_config['on'][$eventName] = '@@function (ev){'; foreach ($handlers as $handler => $code) { $_config['on'][$eventName] .= '('.$code.')(ev);'; } $_config['on'][$eventName] .= '}'; } } } return $_config; } /** * Return global event handlers. */ private function returnGlobalEvents() { static $returnedEvents; $out = ""; if (!isset($returnedEvents)) { $returnedEvents = array(); } if (!empty($this->globalEvents)) { foreach ($this->globalEvents as $eventName => $handlers) { foreach ($handlers as $handler => $code) { if (!isset($returnedEvents[$eventName])) { $returnedEvents[$eventName] = array(); } // Return only new events if (!in_array($code, $returnedEvents[$eventName])) { $out .= ($code ? "\n" : "") . "CKEDITOR.on('". $eventName ."', $code);"; $returnedEvents[$eventName][] = $code; } } } } return $out; } /** * Initializes CKEditor (executed only once). */ private function init() { static $initComplete; $out = ""; if (!empty($initComplete)) { return ""; } if ($this->initialized) { $initComplete = true; return ""; } $args = ""; $ckeditorPath = $this->ckeditorPath(); if (!empty($this->timestamp) && $this->timestamp != "%"."TIMESTAMP%") { $args = '?t=' . $this->timestamp; } // Skip relative paths... if (strpos($ckeditorPath, '..') !== 0) { $out .= $this->script("window.CKEDITOR_BASEPATH='". $ckeditorPath ."';"); } $out .= "<script type=\"text/javascript\" src=\"" . $ckeditorPath . 'ckeditor.js' . $args . "\"></script>\n"; $extraCode = ""; if ($this->timestamp != self::timestamp) { $extraCode .= ($extraCode ? "\n" : "") . "CKEDITOR.timestamp = '". $this->timestamp ."';"; } if ($extraCode) { $out .= $this->script($extraCode); } $initComplete = $this->initialized = true; return $out; } /** * Return path to ckeditor.js. */ private function ckeditorPath() { if (!empty($this->basePath)) { return $this->basePath; } if (isset($_SERVER['SCRIPT_FILENAME'])) { $realPath = dirname($_SERVER['SCRIPT_FILENAME']); } else { /** * realpath - Returns canonicalized absolute pathname */ $realPath = realpath( './' ) ; } $selfPath = dirname($_SERVER['PHP_SELF']); $file = str_replace("\\", "/", __FILE__); if (!$selfPath || !$realPath || !$file) { return "/ckeditor/"; } $documentRoot = substr($realPath, 0, strlen($realPath) - strlen($selfPath)); $fileUrl = substr($file, strlen($documentRoot)); $ckeditorUrl = str_replace("ckeditor_php5.php", "", $fileUrl); return $ckeditorUrl; } /** * This little function provides a basic JSON support. * * @param mixed $val * @return string */ private function jsEncode($val) { if (is_null($val)) { return 'null'; } if (is_bool($val)) { return $val ? 'true' : 'false'; } if (is_int($val)) { return $val; } if (is_float($val)) { return str_replace(',', '.', $val); } if (is_array($val) || is_object($val)) { if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) { return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']'; } $temp = array(); foreach ($val as $k => $v){ $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v); } return '{' . implode(',', $temp) . '}'; } // String otherwise if (strpos($val, '@@') === 0) return substr($val, 2); if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') return $val; return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"'; } } ?> |
Step 7:- Now to go to our project’s library folder and create “CKFinder.php” library file like “htdocs\ci_demo\application\libraries\CKFinder.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
<?php define( 'CKFINDER_DEFAULT_BASEPATH', '/ckfinder/' ) ; class CKFinder { public $BasePath ; public $Width ; public $Height ; public $SelectFunction ; public $SelectFunctionData ; public $SelectThumbnailFunction ; public $SelectThumbnailFunctionData ; public $DisableThumbnailSelection = false ; public $ClassName = '' ; public $Id = '' ; public $StartupPath ; public $ResourceType ; public $RememberLastFolder = true ; public $StartupFolderExpanded = false ; // PHP 5 Constructor function __construct( $basePath = CKFINDER_DEFAULT_BASEPATH, $width = '100%', $height = 400, $selectFunction = null ) { $this->BasePath = $basePath ; $this->Width = $width ; $this->Height = $height ; $this->SelectFunction = $selectFunction ; $this->SelectThumbnailFunction = $selectFunction ; } // Renders CKFinder in the current page. public function Create() { echo $this->CreateHtml() ; } // Gets the HTML needed to create a CKFinder instance. public function CreateHtml() { $className = $this->ClassName ; if ( !empty( $className ) ) $className = ' class="' . $className . '"' ; $id = $this->Id ; if ( !empty( $id ) ) $id = ' id="' . $id . '"' ; return '<iframe src="' . $this->_BuildUrl() . '" width="' . $this->Width . '" ' . 'height="' . $this->Height . '"' . $className . $id . ' frameborder="0" scrolling="no"></iframe>' ; } private function _BuildUrl( $url = "" ) { if ( !$url ) $url = $this->BasePath ; $qs = "" ; if ( empty( $url ) ) $url = CKFINDER_DEFAULT_BASEPATH ; if ( $url[ strlen( $url ) - 1 ] != '/' ) $url = $url . '/' ; $url .= 'ckfinder.html' ; if ( !empty( $this->SelectFunction ) ) $qs .= '?action=js&func=' . $this->SelectFunction ; if ( !empty( $this->SelectFunctionData ) ) { $qs .= $qs ? "&" : "?" ; $qs .= 'data=' . rawurlencode($this->SelectFunctionData) ; } if ( $this->DisableThumbnailSelection ) { $qs .= $qs ? "&" : "?" ; $qs .= "dts=1" ; } else if ( !empty( $this->SelectThumbnailFunction ) || !empty( $this->SelectFunction ) ) { $qs .= $qs ? "&" : "?" ; $qs .= 'thumbFunc=' . ( !empty( $this->SelectThumbnailFunction ) ? $this->SelectThumbnailFunction : $this->SelectFunction ) ; if ( !empty( $this->SelectThumbnailFunctionData ) ) $qs .= '&tdata=' . rawurlencode( $this->SelectThumbnailFunctionData ) ; else if ( empty( $this->SelectThumbnailFunction ) && !empty( $this->SelectFunctionData ) ) $qs .= '&tdata=' . rawurlencode( $this->SelectFunctionData ) ; } if ( !empty( $this->StartupPath ) ) { $qs .= ( $qs ? "&" : "?" ) ; $qs .= "start=" . urlencode( $this->StartupPath . ( $this->StartupFolderExpanded ? ':1' : ':0' ) ) ; } if ( !empty( $this->ResourceType ) ) { $qs .= ( $qs ? "&" : "?" ) ; $qs .= "type=" . urlencode( $this->ResourceType ) ; } if ( !$this->RememberLastFolder ) { $qs .= ( $qs ? "&" : "?" ) ; $qs .= "rlf=0" ; } if ( !empty( $this->Id ) ) { $qs .= ( $qs ? "&" : "?" ) ; $qs .= "id=" . urlencode( $this->Id ) ; } return $url . $qs ; } // Static "Create". public static function CreateStatic( $basePath = CKFINDER_DEFAULT_BASEPATH, $width = '100%', $height = 400, $selectFunction = null ) { $finder = new CKFinder( $basePath, $width, $height, $selectFunction ) ; $finder->Create() ; } // Static "SetupFCKeditor". public static function SetupFCKeditor( &$editorObj, $basePath = CKFINDER_DEFAULT_BASEPATH, $imageType = null, $flashType = null ) { if ( empty( $basePath ) ) $basePath = CKFINDER_DEFAULT_BASEPATH ; $ckfinder = new CKFinder( $basePath ) ; $ckfinder->SetupFCKeditorObject( $editorObj, $imageType, $flashType ); } // Non-static method of attaching CKFinder to FCKeditor public function SetupFCKeditorObject( &$editorObj, $imageType = null, $flashType = null ) { $url = $this->BasePath ; // If it is a path relative to the current page. if ( isset($url[0]) && $url[0] != '/' ) { $url = substr( $_SERVER[ 'REQUEST_URI' ], 0, strrpos( $_SERVER[ 'REQUEST_URI' ], '/' ) + 1 ) . $url ; } $url = $this->_BuildUrl( $url ) ; $qs = ( strpos($url, "?") !== false ) ? "&" : "?" ; if ( $this->Width !== '100%' && is_numeric( str_ireplace( "px", "", $this->Width ) ) ) { $width = intval( $this->Width ); $editorObj->Config['LinkBrowserWindowWidth'] = $width ; $editorObj->Config['ImageBrowserWindowWidth'] = $width ; $editorObj->Config['FlashBrowserWindowWidth'] = $width ; } if ( $this->Height !== 400 && is_numeric( str_ireplace( "px", "", $this->Height ) ) ) { $height = intval( $this->Height ); $editorObj->Config['LinkBrowserWindowHeight'] = $height ; $editorObj->Config['ImageBrowserWindowHeight'] = $height ; $editorObj->Config['FlashBrowserWindowHeight'] = $height ; } $editorObj->Config['LinkBrowserURL'] = $url ; $editorObj->Config['ImageBrowserURL'] = $url . $qs . 'type=' . ( empty( $imageType ) ? 'Images' : $imageType ) ; $editorObj->Config['FlashBrowserURL'] = $url . $qs . 'type=' . ( empty( $flashType ) ? 'Flash' : $flashType ) ; $dir = substr( $url, 0, strrpos( $url, "/" ) + 1 ) ; $editorObj->Config['LinkUploadURL'] = $dir . urlencode( 'core/connector/php/connector.php?command=QuickUpload&type=Files' ) ; $editorObj->Config['ImageUploadURL'] = $dir . urlencode( 'core/connector/php/connector.php?command=QuickUpload&type=') . ( empty( $imageType ) ? 'Images' : $imageType ) ; $editorObj->Config['FlashUploadURL'] = $dir . urlencode( 'core/connector/php/connector.php?command=QuickUpload&type=') . ( empty( $flashType ) ? 'Flash' : $flashType ) ; } // Static "SetupCKEditor". public static function SetupCKEditor( &$editorObj, $basePath = CKFINDER_DEFAULT_BASEPATH, $imageType = null, $flashType = null ) { if ( empty( $basePath ) ) $basePath = CKFINDER_DEFAULT_BASEPATH ; $ckfinder = new CKFinder( $basePath ) ; $ckfinder->SetupCKEditorObject( $editorObj, $imageType, $flashType ); } // Non-static method of attaching CKFinder to CKEditor public function SetupCKEditorObject( &$editorObj, $imageType = null, $flashType = null ) { $url = $this->BasePath ; // If it is a path relative to the current page. if ( isset($url[0]) && $url[0] != '/' ) { $url = substr( $_SERVER[ 'REQUEST_URI' ], 0, strrpos( $_SERVER[ 'REQUEST_URI' ], '/' ) + 1 ) . $url ; } $url = $this->_BuildUrl( $url ) ; $qs = ( strpos($url, "?") !== false ) ? "&" : "?" ; if ( $this->Width !== '100%' && is_numeric( str_ireplace( "px", "", $this->Width ) ) ) { $width = intval( $this->Width ); $editorObj->config['filebrowserWindowWidth'] = $width ; } if ( $this->Height !== 400 && is_numeric( str_ireplace( "px", "", $this->Height ) ) ) { $height = intval( $this->Height ); $editorObj->config['filebrowserWindowHeight'] = $height ; } $editorObj->config['filebrowserBrowseUrl'] = $url ; $editorObj->config['filebrowserImageBrowseUrl'] = $url . $qs . 'type=' . ( empty( $imageType ) ? 'Images' : $imageType ) ; $editorObj->config['filebrowserFlashBrowseUrl'] = $url . $qs . 'type=' . ( empty( $flashType ) ? 'Flash' : $flashType ) ; $dir = substr( $url, 0, strrpos( $url, "/" ) + 1 ) ; $editorObj->config['filebrowserUploadUrl'] = $dir . 'core/connector/php/connector.php?command=QuickUpload&type=Files' ; $editorObj->config['filebrowserImageUploadUrl'] = $dir . 'core/connector/php/connector.php?command=QuickUpload&type=' . ( empty( $imageType ) ? 'Images' : $imageType ) ; $editorObj->config['filebrowserFlashUploadUrl'] = $dir . 'core/connector/php/connector.php?command=QuickUpload&type=' . ( empty( $flashType ) ? 'Flash' : $flashType ) ; } } ?> |
Step 8:- Now we need to go to our project’s helpers folder and create “ckeditor_helper.php” library file like “htdocs\ci_demo\application\helpers\ckeditor_helper.php” and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php if(!defined('BASEPATH')) exit('No direct script access allowed'); function cke_initialize($data = array()) { $return = ''; if(!defined('CI_CKEDITOR_HELPER_LOADED')) { define('CI_CKEDITOR_HELPER_LOADED', TRUE); $return = '<script type="text/javascript" src="'.base_url(). $data['path'] . '/ckeditor.js"></script>'; $return .= "<script type=\"text/javascript\">CKEDITOR_BASEPATH = '" . base_url() . $data['path'] . "/';</script>"; } return $return; } function cke_create_instance($data = array()) { $return = "<script type=\"text/javascript\"> CKEDITOR.replace('" . $data['id'] . "', {"; //Adding config values if(isset($data['config'])) { foreach($data['config'] as $k=>$v) { // Support for extra config parameters if (is_array($v)) { $return .= $k . " : ["; $return .= config_data($v); $return .= "]"; } else { $return .= $k . " : '" . $v . "'"; } if($k !== end(array_keys($data['config']))) { $return .= ","; } } } $return .= '});</script>'; return $return; } function display_ckeditor($data = array()) { // Initialization $return = cke_initialize($data); // Creating a Ckeditor instance $return .= cke_create_instance($data); // Adding styles values if(isset($data['styles'])) { $return .= "<script type=\"text/javascript\">CKEDITOR.addStylesSet( 'my_styles_" . $data['id'] . "', ["; foreach($data['styles'] as $k=>$v) { $return .= "{ name : '" . $k . "', element : '" . $v['element'] . "', styles : { "; if(isset($v['styles'])) { foreach($v['styles'] as $k2=>$v2) { $return .= "'" . $k2 . "' : '" . $v2 . "'"; if($k2 !== end(array_keys($v['styles']))) { $return .= ","; } } } $return .= '} }'; if($k !== end(array_keys($data['styles']))) { $return .= ','; } } $return .= ']);'; $return .= "CKEDITOR.instances['" . $data['id'] . "'].config.stylesCombo_stylesSet = 'my_styles_" . $data['id'] . "'; </script>"; } return $return; } function config_data($data = array()) { $return = ''; foreach ($data as $key) { if (is_array($key)) { $return .= "["; foreach ($key as $string) { $return .= "'" . $string . "'"; if ($string != end(array_values($key))) $return .= ","; } $return .= "]"; } else { $return .= "'".$key."'"; } if ($key != end(array_values($data))) $return .= ","; } return $return; } ?> |
Step 9:- At Last for run your project open browser and type in URL like “localhost/your_project_name/ckeditor/ckeditorController”
like
localhost/ci_demo/ckeditor/ckeditorController
you can see the result as we can see in below snapshot
Congratulations you have successfully Completed “CKeditor integration process in Codeigniter”, if this post was helpful for you then please share this post on social media and if you have any query then please contact us or comment below and don’t forget us follow and like on Social Media, Thanks.
5 Comments:
Akmal June 27, 2018
Hi, can we use CKEditor 5 with this?
Webpreparations Team July 21, 2018
Yes Sure dude 🙂
Mudassar hanif July 25, 2018
nice lets integrate in my website thanks
david fernandez February 04, 2019
work perfect 😀
Webpreparations Team February 04, 2019
Thank you for your appreciation, please like us on social media.