<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://docs-old.ckeditor.com/index.php?action=history&amp;feed=atom&amp;title=CKEditor_3.x%2FTutorials%2FTimestamp_Plugin</id>
		<title>CKEditor 3.x/Tutorials/Timestamp Plugin - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://docs-old.ckeditor.com/index.php?action=history&amp;feed=atom&amp;title=CKEditor_3.x%2FTutorials%2FTimestamp_Plugin"/>
		<link rel="alternate" type="text/html" href="https://docs-old.ckeditor.com/index.php?title=CKEditor_3.x/Tutorials/Timestamp_Plugin&amp;action=history"/>
		<updated>2026-05-16T17:54:29Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.1</generator>

	<entry>
		<id>https://docs-old.ckeditor.com/index.php?title=CKEditor_3.x/Tutorials/Timestamp_Plugin&amp;diff=6160&amp;oldid=prev</id>
		<title>Anna: Article contents added</title>
		<link rel="alternate" type="text/html" href="https://docs-old.ckeditor.com/index.php?title=CKEditor_3.x/Tutorials/Timestamp_Plugin&amp;diff=6160&amp;oldid=prev"/>
				<updated>2011-04-14T12:53:32Z</updated>
		
		<summary type="html">&lt;p&gt;Article contents added&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{#CUSTOMTITLE:Creating a CKEditor Plugin in 20 Lines of Code}}&lt;br /&gt;
__TOC__&lt;br /&gt;
The aim of this tutorial is to demonstrate how to create a most basic CKEditor plugin.&lt;br /&gt;
 &lt;br /&gt;
We are going to develop a '''timestamp plugin''' that inserts current date and time into the editing area of CKEditor. The timestamp will be added after a user clicks a dedicated toolbar button. Since the implementation makes use of the &amp;lt;code&amp;gt;insertHtml&amp;lt;/code&amp;gt; function, this example can be easily adjusted to insert any other HTML element.&lt;br /&gt;
&lt;br /&gt;
The plugin will be named &amp;lt;code&amp;gt;'''timestamp'''&amp;lt;/code&amp;gt; and according to CKEditor [[FCKeditor 3.x/Design and Architecture/Coding Style|naming conventions]], this name will also be given to the plugin folder.&lt;br /&gt;
&lt;br /&gt;
== Plugin Files ==&lt;br /&gt;
Firstly, we will need to create the &amp;lt;code&amp;gt;timestamp&amp;lt;/code&amp;gt; folder inside the &amp;lt;code&amp;gt;plugins&amp;lt;/code&amp;gt; directory of the CKEditor installation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;note&amp;gt;Remember that for CKEditor the name of the plugin folder is important and has to be the same as the name of the plugin, otherwise the editor will not be able to recognize it.&amp;lt;/note&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inside the newly created &amp;lt;code&amp;gt;timestamp&amp;lt;/code&amp;gt; folder we are going to place the &amp;lt;code&amp;gt;plugin.js&amp;lt;/code&amp;gt; file that will contain the plugin logic. Apart from that, since we will also need a toolbar icon for our plugin, we are going to add an &amp;lt;code&amp;gt;images&amp;lt;/code&amp;gt; folder and subsequently place the &amp;lt;code&amp;gt;timestamp.png&amp;lt;/code&amp;gt; file inside.&lt;br /&gt;
&lt;br /&gt;
To sum up, we will need the following file structure for our plugin to work:&lt;br /&gt;
* &amp;lt;code&amp;gt;''ckeditor root''&amp;lt;/code&amp;gt;&lt;br /&gt;
** &amp;lt;code&amp;gt;plugins&amp;lt;/code&amp;gt;&lt;br /&gt;
*** &amp;lt;code&amp;gt;timestamp&amp;lt;/code&amp;gt;&lt;br /&gt;
**** &amp;lt;code&amp;gt;images&amp;lt;/code&amp;gt;&lt;br /&gt;
***** &amp;lt;code&amp;gt;timestamp.png&amp;lt;/code&amp;gt;&lt;br /&gt;
**** &amp;lt;code&amp;gt;plugin.js&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Plugin Source Code ==&lt;br /&gt;
With the following structure ready, it is time to open the &amp;lt;code&amp;gt;plugin.js&amp;lt;/code&amp;gt; file in a text editor and to start creating the source code of the plugin.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
CKEDITOR.plugins.add( 'timestamp',&lt;br /&gt;
{&lt;br /&gt;
	init: function( editor )&lt;br /&gt;
	{&lt;br /&gt;
		//Plugin logic goes here.&lt;br /&gt;
	}&lt;br /&gt;
} );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
All CKEditor plugins are created by using the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.resourceManager.html#add CKEDITOR.plugins.add]&amp;lt;/code&amp;gt; function. This function should contain the plugin name (&amp;lt;code&amp;gt;'timestamp'&amp;lt;/code&amp;gt;) and the plugin logic placed inside the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.pluginDefinition.html#init init]&amp;lt;/code&amp;gt; function that is called upon the initialization of the editor instance.&lt;br /&gt;
&lt;br /&gt;
=== Creating an Editor Command ===&lt;br /&gt;
It is customary for CKEditor plugins to define an editor command that performs an action associated with them. The command should be defined inside the &amp;lt;code&amp;gt;init&amp;lt;/code&amp;gt; function in order to be called upon the initialization of a CKEditor instance.&lt;br /&gt;
&lt;br /&gt;
In this case we are going to use the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#addCommand  addCommand]&amp;lt;/code&amp;gt; function to define an &amp;lt;code&amp;gt;insertTimestamp&amp;lt;/code&amp;gt; command that, as the name suggests, will insert the current date and time into a document created in CKEditor.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.commandDefinition.html#exec exec]&amp;lt;/code&amp;gt; method defines a function that will be fired when the &amp;lt;code&amp;gt;insertTimestamp&amp;lt;/code&amp;gt; command is executed. Current date and time will be calculated using the JavaScript &amp;lt;code&amp;gt;[https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date Date]&amp;lt;/code&amp;gt; object.&lt;br /&gt;
&lt;br /&gt;
The most important part is to insert the HTML code into the document. To do this, we will use the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#insertHtml insertHtml]&amp;lt;/code&amp;gt; function from [http://docs.cksource.com/ckeditor_api/ CKEditor JavaScript API]. This function can be used to insert arbitrary HTML code into the document, so with a bit of tweaking you can customize the timestamp plugin code to add other HTML elements into the CKEditor editing area.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
editor.addCommand( 'insertTimestamp',&lt;br /&gt;
	{&lt;br /&gt;
		exec : function( editor )&lt;br /&gt;
		{    &lt;br /&gt;
			var timestamp = new Date();&lt;br /&gt;
			editor.insertHtml( 'The current date and time is: &amp;lt;em&amp;gt;' + timestamp.toString() + '&amp;lt;/em&amp;gt;' );&lt;br /&gt;
		}&lt;br /&gt;
	});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;note&amp;gt;Please note that there are other ways to insert code into CKEditor. Check the [http://docs.cksource.com/ckeditor_api/ CKEditor API Documentation] for more methods that let you add elements and do other useful things with CKEditor.&amp;lt;/note&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Creating a Toolbar Button ===&lt;br /&gt;
The simplified timestamp plugin should work through a toolbar button. To this end, inside the &amp;lt;code&amp;gt;init&amp;lt;/code&amp;gt; function we need to define a button that will be associated with the &amp;lt;code&amp;gt;insertTimestamp&amp;lt;/code&amp;gt; command. The &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.ui.html#addButton CKEditor.ui.addButton]&amp;lt;/code&amp;gt; function accepts a button name (&amp;lt;code&amp;gt;'Timestamp'&amp;lt;/code&amp;gt;) along with the definition of the tooltip text (&amp;lt;code&amp;gt;label&amp;lt;/code&amp;gt;) and the button icon (&amp;lt;code&amp;gt;icon&amp;lt;/code&amp;gt;). Note that &amp;lt;code&amp;gt;this.path&amp;lt;/code&amp;gt; is the directory where the &amp;lt;code&amp;gt;plugin.js&amp;lt;/code&amp;gt; file resides.&lt;br /&gt;
&lt;br /&gt;
These parameters are responsible for the button presentation. To make the button actually work, we need to connect it to the plugin command name defined above by using the &amp;lt;code&amp;gt;command&amp;lt;/code&amp;gt; parameter.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
editor.ui.addButton( 'Timestamp',&lt;br /&gt;
{&lt;br /&gt;
	label: 'Insert Timestamp',&lt;br /&gt;
	command: 'insertTimestamp',&lt;br /&gt;
	icon: this.path + 'images/timestamp.png'&lt;br /&gt;
} );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== CKEditor Initialization ===&lt;br /&gt;
It is now time to initialize a CKEditor instance that will use the Timestamp plugin along with its toolbar button.&lt;br /&gt;
&lt;br /&gt;
To register the plugin with CKEditor, we have to add its name to the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.extraPlugins extraPlugins]&amp;lt;/code&amp;gt; list. We also need to enhance the toolbar definition and add the plugin button name by using the &amp;lt;code&amp;gt;[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.toolbar toolbar]&amp;lt;/code&amp;gt; parameter. &lt;br /&gt;
&lt;br /&gt;
Open the page that will contain CKEditor in a text editor and insert a CKEditor instance using the following toolbar and plugin configuration.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
//&amp;lt;![CDATA[&lt;br /&gt;
	// Replace the &amp;lt;textarea id=&amp;quot;editor1&amp;quot;&amp;gt; with a CKEditor&lt;br /&gt;
	// instance, using default configuration.&lt;br /&gt;
	CKEDITOR.replace( 'editor1',&lt;br /&gt;
		{&lt;br /&gt;
			extraPlugins : 'timestamp',&lt;br /&gt;
			toolbar :&lt;br /&gt;
			[&lt;br /&gt;
				[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],&lt;br /&gt;
				[ 'Timestamp' ]&lt;br /&gt;
			]&lt;br /&gt;
		});&lt;br /&gt;
//]]&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After you load the page containing the above CKEditor instance, you should be able to see the new plugin toolbar button along with its tooltip.&lt;br /&gt;
&lt;br /&gt;
[[Image:timestamp_button_added.png|center|frame|Timestamp plugin button added to CKEditor toolbar]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Full Source Code ==&lt;br /&gt;
To see the full contents of the &amp;lt;code&amp;gt;plugin.js&amp;lt;/code&amp;gt; file, &amp;lt;hide target=&amp;quot;timestamp_full&amp;quot;&amp;gt;click here&amp;lt;/hide&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;note&amp;gt;You can also [[Media:timestamp.zip|download the whole plugin folder]] inluding the icon and the fully commented source code.&lt;br /&gt;
&amp;lt;/note&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;timestamp_full&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
CKEDITOR.plugins.add( 'timestamp',&lt;br /&gt;
{&lt;br /&gt;
	init: function( editor )&lt;br /&gt;
	{&lt;br /&gt;
		editor.addCommand( 'insertTimestamp',&lt;br /&gt;
			{&lt;br /&gt;
				exec : function( editor )&lt;br /&gt;
				{    &lt;br /&gt;
					var timestamp = new Date();&lt;br /&gt;
					editor.insertHtml( 'The current date and time is: &amp;lt;em&amp;gt;' + timestamp.toString() + '&amp;lt;/em&amp;gt;' );&lt;br /&gt;
				}&lt;br /&gt;
			});&lt;br /&gt;
		editor.ui.addButton( 'Timestamp',&lt;br /&gt;
		{&lt;br /&gt;
			label: 'Insert Timestamp',&lt;br /&gt;
			command: 'insertTimestamp',&lt;br /&gt;
			icon: this.path + 'images/timestamp.png'&lt;br /&gt;
		} );&lt;br /&gt;
	}&lt;br /&gt;
} );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Working Example ==&lt;br /&gt;
The plugin code is now ready. When you click the '''Insert Timestamp''' toolbar button, current date and time will be inserted into the document. Note that the format of the timestamp may vary for different platforms and browsers. If you want to have more control over the display of the date and time, use the &amp;lt;code&amp;gt;[https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date Date]&amp;lt;/code&amp;gt; object's properties and methods.&lt;br /&gt;
&lt;br /&gt;
[[Image:timestamp_example.png|center|frame|Date and time added to the document in CKEditor]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Congratulations! You have just created a valid CKEditor plugin in under 20 lines of code! Since the &amp;lt;code&amp;gt;insertHtml&amp;lt;/code&amp;gt; function can be used to add arbitrary HTML code to the document, you can replace the timestamp logic with your own customized code in order to insert other types of content into your document.&lt;/div&gt;</summary>
		<author><name>Anna</name></author>	</entry>

	</feed>