{"id":359,"date":"2011-03-17T00:51:33","date_gmt":"2011-03-17T07:51:33","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=359"},"modified":"2011-03-17T00:51:33","modified_gmt":"2011-03-17T07:51:33","slug":"359","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2011\/03\/17\/359\/","title":{"rendered":"How to create a new toolbar plugin in CKEditor"},"content":{"rendered":"<p>Hey there, here is something which I learned the hard way while I was trying out my hands on customizing CKEdior for a website, in which I needed to add a new button which enabled the admin to add a particular kind of text or HTML tag in the textarea.<\/p>\n<p>So therefore I thought I would share my experience through a simple step by step process of how I managed to achieve the customized feature of adding a new plugin (new button) in the CKEditor toolbar.<\/p>\n<p>Well I just made a button which when clicked will add an image (in this case Truelogic.org Logo) in the textarea content.<br \/>\n<span style=\"font: bold 18px arial;\">Step 1:<\/span> The basic directory structure of the CKEditor 3.3<\/p>\n<p><em><span style=\"color: #f40000;\">( DO KEEP IN MIND THE VERSION OF THE CKEditor BEING USED HERE, DIFFERENT VERSIONS MAY HAVE DIFFERENT FOLDER\/DIRECTORY STRUCTURE ) <\/span><\/em><\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px; overflow-x:scroll;\">\n<pre>  ckeditor\/\n        config.js\n        plugins\/\n             insert_image\/\n                    insert_image_button.png\n                    plugin.js<\/pre>\n<\/div>\n<p><span style=\"font: bold 18px arial;\">Step 2:<\/span> Now, as you may have an idea after seeing the folder structure that you have to create a new folder called \u201cinsert_image\u201d, inside the plugins folder.<\/p>\n<p><em><span style=\"color: #f40000;\">( <strong>IMPORTANT:<\/strong> Folder name is very important because the config.js recognizes the function of the plugin based on the folder name given )<\/span><\/em><\/p>\n<p>Now the code which you have to paste after creating the <strong><em>\u201cplugin.js\u201d<\/em><\/strong> inside the <strong><em>\u201cinsert_image\u201d<\/em><\/strong> folder is given below , this plugin code allows you to insert a predefined image inside the textarea <em>(in this case Truelogic.org Logo)<\/em><\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px; overflow-x:scroll;\">\n<pre>(function(){\n     \/\/The Code is written here, which will be executed when the button is clicked\n     var a= {\n     exec:function(editor){\n             CKEDITOR.instances.txtDescrip.insertHtml(\"&lt;img src='http:\/\/truelogic.org\/simple\/images\/true.png' border=0 \/&gt;\");  \n     \/\/CKEDITOR.instances.ID OF THE TEXTAREA.insertHtml(\"IMAGE URL\");\n     \/\/above line inserts an IMG tag with the predefined url of the image which points to the truelogic logo\n              }\n},\n\n\/\/The actual button is defined\/created here and the associated code is linked with it\nb='insert_image';\nCKEDITOR.plugins.add(b,{\n    init:function(editor){\n    editor.addCommand(b,a); \/\/ associating the executable code with the button\n    editor.ui.addButton('insert_image',{\n                                     label:'Insert image',\n                                     icon: this.path + 'insert_image_button.png',\n                          \/\/defining the path of the icon image which will be displayed on the toolbar for this button\n                          \/\/easiest would be to have the icon image in the same folder as the plugin.js\n\n                                     command:b\n                             });\n   }\n});\n})();<\/pre>\n<\/div>\n<p><span style=\"font: bold 18px arial;\">Step 3:<\/span><\/p>\n<p>Here is the \u201cinsert_image_button.png\u201d image to make things easier for you guys <a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/insert_image_button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-363\" title=\"insert_image_button\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/insert_image_button.png\" alt=\"\" width=\"17\" height=\"18\" \/><\/a><\/p>\n<p>This image has to be put inside the <strong>\u201cplugins\/insert_image\/\u201d<\/strong> folder, next to the plugin.js<\/p>\n<p><span style=\"font: bold 18px arial;\">Step 4:<\/span><br \/>\nHere is the code which is going in the config.js:<\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px; overflow-x:scroll;\">\n<pre>CKEDITOR.editorConfig = function( config )\n{\n     CKEDITOR.config.toolbar_Basic = [\n                  ['Source','-','Templates'],\n                  ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],\n                  ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],\n                  ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],\n                  '\/',\n                  ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],\n                  ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],\n                  ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],\n                  ['Link','Unlink','Anchor'],\n                  ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],\n                  '\/',\n                  ['Styles','Format','Font','FontSize'],\n                  ['TextColor','BGColor'],\n                  ['Maximize', 'ShowBlocks','-','About'],\n                  ['insert_image'] \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/Button added to the toolbar\n                ]\n config.toolbar = 'Basic';\n config.extraPlugins = \"insert_image\u201d;\u00a0\u00a0 \/\/registering the plugin\n};<\/pre>\n<\/div>\n<p><span style=\"font: bold 18px arial;\">Tips:<\/span><\/p>\n<p>If you don\u2019t want to alter the default setup of the CKEditor toolbar or if you want to show or hide specific plugins on particular pages or just one page, you can do it by writing a few lines of code in the page where you want to make this happen.<\/p>\n<p>For you to understand more clearly, let me give you an example, say I only want around 10 buttons to be displayed in the toolbar for a specific page called \u201ctest-editor.html\u201d<\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px; overflow-x:scroll;\">\n<pre>&lt;script type=\"text\/javascript\"&gt;\n\nvar editor = CKEDITOR.replace( 'txtDescrip',    \/\/var editor = CKEDITOR.replace( 'ID OF THE TEXTAREA', \n{\n                toolbar : [\n                         ['Source','-','Templates'],\n                         ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],\n                         '\/',\n                         ['insert_image']\n                            ]\n });\n&lt;\/script&gt;<\/pre>\n<\/div>\n<p>Using the above code and pasting it in the page where you want to change the look of the toolbar (hide or display a few of the buttons) will allow the page to only show those buttons or plugins which are mentioned in the code<\/p>\n<p>Just to make it even more easier , here is the full code of the <strong>\u201ctest-editor.html\u201d<\/strong><\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px; overflow-x:scroll;\">\n<pre>&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 strict\/\/EN\"&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Truelogic.org::TestEditor::CKEditor example &lt;\/title&gt;\n\n&lt;script type=\"text\/javascript\" src=\"ckeditor\/ckeditor.js\"&gt;&lt;\/script&gt;\n \/\/ do keep in mind the include path of the CKEditor folder on the web server or the local server\n\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n &lt;form name=\"\" action=\"\" method=\"\"&gt;\n &lt;textarea id=\"txtDescrip\" cols=\"20\" rows=\"5\"&gt; &lt;\/textarea&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n var editor = CKEDITOR.replace( 'txtDescrip',   \/\/var editor = CKEDITOR.replace( 'ID OF THE TEXTAREA', \n {\n toolbar : [\n ['Source','-','Templates'],\n ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],\n ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],\n ['Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton','HiddenField'],\n '\/',\n ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],\n ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],\n ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],\n ['Link','Unlink','Anchor'],\n ['Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],\n '\/',\n ['Styles','Format','Font','FontSize'],\n ['TextColor','BGColor'],\n ['Maximize', 'ShowBlocks','-','About'],['insert_image'] ]\n });\n&lt;\/script&gt;\n\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<\/div>\n<p><span style=\"font: bold 18px arial;\">Small Hints:<\/span><\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px;\">&#8216;\/&#8217;,<\/div>\n<p>&#8211; this allows you to add a line break in the middle of the toolbar so that whatever plugin is written after this is displayed in the next line of the toolbar<\/p>\n<div style=\"background: black; color: #f2f2f2; font-family: courier; padding: 8px;\">&#8216;-&#8216;<\/div>\n<p>&#8211; this allows you to add a small gap between the buttons.<br \/>\n<em>Like for example: the gap between \u2018strike-through\u2019 button and the \u2018SubScript\u2019 button<\/em><\/p>\n<p>Preview of the CKEditor (default) <em>Before<\/em> adding the new plugin<\/p>\n<figure id=\"attachment_360\" aria-describedby=\"caption-attachment-360\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_default.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-360\" title=\"ckeditor_default\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_default-300x52.png\" alt=\"\" width=\"300\" height=\"52\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_default-300x52.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_default-500x87.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_default.png 741w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-360\" class=\"wp-caption-text\">CKEditor - default toolbar<\/figcaption><\/figure>\n<p>Preview of the CKEditor (customized) <em>After<\/em> adding the new plugin<\/p>\n<figure id=\"attachment_361\" aria-describedby=\"caption-attachment-361\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_added.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-361\" title=\"ckeditor_button_added\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_added-300x57.png\" alt=\"\" width=\"300\" height=\"57\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_added-300x57.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_added-500x95.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_added.png 871w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-361\" class=\"wp-caption-text\">CKEditor - customized toolbar with new plugin<\/figcaption><\/figure>\n<p>Preview of the textarea when the new plugin button has been clicked<\/p>\n<figure id=\"attachment_362\" aria-describedby=\"caption-attachment-362\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_clicked.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-362\" title=\"ckeditor_button_clicked\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_clicked-300x148.png\" alt=\"\" width=\"300\" height=\"148\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_clicked-300x148.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_clicked-500x247.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2011\/03\/ckeditor_button_clicked.png 732w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-362\" class=\"wp-caption-text\">CKEditor - when insert_image button has been clicked<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Hey there, here is something which I learned the hard way while I was trying out my hands on customizing CKEdior for a website, in <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2011\/03\/17\/359\/\" title=\"How to create a new toolbar plugin in CKEditor\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,8,10,11],"tags":[43,58,67,78,83,86,207,208,252],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-javascript","category-misc","category-programming","category-tutorials","tag-arunace","tag-button","tag-ckeditor","tag-config-js","tag-create","tag-customize","tag-plugin","tag-plugin-js","tag-toolbar"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}