How do I add or remove plugins in CKEditor without rebuilding? - ckeditor

I've just started using CKEditor 4 (having previously used version 1 a long time ago). I like that I can build it online and download it, but when I do that, I then use the toolbar config tool to set up my toolbar.
What happens if I want to add or remove a specific plugin in future though? Will I have to build a completely new CKEditor using the build tool, then download it to replace the existing one, and then reconfigure my toolbar? I don't really want to have to reconfig the toolbar each time.
There are a couple of plugins that I might want to use later, so I'm just trying to figure out whether I need to include them now, or can I add them in with no hassle later on?

Remove plugin
Removing is quite easy. CKEditor provide configuration option where you can define plugins to be remove. https://docs.ckeditor.com/ckeditor4/docs/#!/api/CKEDITOR.config-cfg-removePlugins
E.g.
CKEDITOR.replace( 'editor', {
removePlugins: 'basicstyles,justify'
} );
You need to remember that, removing plugins might break dependancies. E.g. you wish to remove clipboard plugin, but you want to load pastefromword plugin. Paste from Word requires clipboard for proper work, removing clipboard will break loading this plugin. Adequate error will be thrown in the console.
Plugin option
Alternative solution is to define plugins which you wish to load in editor. You need to use plugin option in configuration https://docs.ckeditor.com/ckeditor4/docs/#!/api/CKEDITOR.config-cfg-plugins. This will only load defined plugins together with its dependancies. E.g. In case like above, when you define pastefromword plugin to be loaded, this will also load clipboard plugin.
Adding plugin
There is configuration option for loading extra plugins. Where you can define names of plugins to be loaded: https://docs.ckeditor.com/ckeditor4/docs/#!/api/CKEDITOR.config-cfg-extraPlugins. Here situation is a little bit more complicated because plugin needs to be available for the editor. When you wish to load plugin you need to make 2 things:
Load plugin (more detail description is below)
Add plugin for editor instance, with extraPlugins configuration option.
There might be situation that you have few editors on one page and every of editor will have different available plugins. In such case all plugins will be loaded, but no all will be used in specific editor instance.
Define plugin inline in code
If you wrote your own plugin you might want to define it directly in JS. You just need to take care to be defined before initialisation of the editor.
https://codepen.io/msamsel/pen/NwGJYL
CKEDITOR.plugins.add( 'testplugin', {
init: function( editor ) {
console.log( 'plugin loaded' );
// adding more logic
}
} )
CKEDITOR.replace( 'editor', {
extraPlugins: 'testplugin'
} );
Load plugin from local resources
If you wish to load plugin which you download/create separately, you can create proper folder structure together with CKEditor. Such added plugins will be available and possible to add through extraPlugins.
ckeditor root/
plugins/
<plugin name>/
icons/
<plugin name>.png
dialogs/
<dialog file>.js
plugin.js
A little bit more you can find at the beginning of tutorial for creating plugins: https://docs.ckeditor.com/ckeditor4/docs/#!/guide/plugin_sdk_sample_1
Load plugin from external resources
Plugin might be also loaded form external sources through this method https://docs.ckeditor.com/ckeditor4/docs/#!/api/CKEDITOR.resourceManager-method-addExternal
E.g.
CKEDITOR.plugins.addExternal( 'timestamp', 'https://sdk.ckeditor.com/samples/assets/plugins/timestamp/', 'plugin.js' );
CKEDITOR.replace( 'editor1', {
extraPlugins: 'timestamp'
} );

Related

Does CKEditor's "config.removePlugins" setting affect its loading time?

I have a custom CKEditor build. Eventually, after I've done code changes, which I don't want to lose, I decided that not all of the plugins, which I included in the initial build, were necessary, so I removed some of them through the config.js file by using config.removePlugins.
My questions are:
Does this method improve the loading speed of the editor at all or does it remove the plugins after they have been loaded?
If the first is true, does it only affect the loading of resources from the plugins folder or does it also affect the loaded content of the ckeditor.js file?
If you downloaded the Full package from CKEditor download page then while loading the editor, the whole package gets loaded even if config.removePlugins is used.
The less plugins you have, the faster the editor loads. This is the general rule. Another one is that the editor should be served in release and not the source mode.
The best practice is to get the editor source code from here, create your own fork which you can update to newest stable branches, create your custom plugins and build your own custom editor release according to this link.
This way you will get all the plugins you want and the editor code will be minified, obfuscated and combined into a single ckeditor.js file which should guarantee the fastest load time.

How do I add a CKEditor plugin from within a plugin?

I am developing a CKEditor plugin that I would like to be vehicle to deliver multiple plugins all required for the correct editing experience. Basically I want the user to be able to include a single "the_framework" plugin, which in turn adds 10 more plugins (each a widget with toolbar button and possibly context menu) that provides the full editing support. I do not want the "plugin" configuration to be responsible for loading these plugins. It's really an all or nothing proposition in my case.
I've tried having the master plugin add external plugins with CKEDITOR.plugins.addExternal but they don't load. I realize now this is because the plugins or extraPlugins configuration doesn't name the external plugins so Editor.loadPlugins never tries them. However, by the time my master plugin loads and executes onLoad it's already too late to change the configuration.
I'm fine with having multiple plugin definitions in one plugin file, but it appears it still won't work if the editor config doesn't explicitly name them all.
How can I load a plugin from another plugin?
After some more debugging, I have determined this to be impossible.
I realized to load the "sub-plugins" registered as externals I simply needed to call CKEDITOR.plugins.load with the names of the plugins after registering them. That is the same method called by the editor, however, the editor follows up with a lot of extra steps including loading language files and critical callbacks (beforeInit, init, and afterInit).
The editor.js loadPlugins function is where all the action occurs, but it only accepts an Editor argument without a list of plugins to load, because the editor's config is used for determining which plugins to load. Once the list is determined, they are all loaded in parallel. If the sub-plugins are not in this array from the beginning, even if the master plugin loads them manually later, they will never be fully initialized with an editor.
The only work-around I have come up with involves loading one extra script after loading the CKEditor source, ideally a script distributed with the master plugin. The script sets up the external sub-plugin definitions (which are actually in a local subfolder named "master_plugin/plugins") and registers an instanceCreated callback, which basically looks at the a new editor's config and determines if the sub-plugins need to be added to the list before the editor initializes. If any of this occurs from within a plugin, vs the extra script, it is too late and it will fail.
UPDATE
It was actually necessary to register a configLoaded listener inside the instanceCreated callback, because when instanceCreated occurs the user's config is not yet loaded into the editor. The customConfigLoaded event is also too early to receive the user's config, so any modifications to plugins or extendedPlugins will still be lost. Surprisingly, the configLoaded event is fired after the editor makes use of several config options (readOnly, enterMode, shiftEnterMode, tabIndex, skin, maybe others), but plugins is not yet touched.
The configLoaded event is the only hook point I could find to guarantee edits to the page-supplied config. With the config.customConfig option, it would be possible to listen for customConfigLoaded within the editorConfig function and get the full config, because CKEditor only merges the page config inside its own customConfigLoaded listener. (The order of operations makes this possible.)
Returning to the original goal of loading many plugins through a single plugin (add-on package), it is possible but obviously nothing intended by the design of the plugin system.

Have your custom theme exported in SemanticUI

Following documentation, I downloaded Semantic UI with:
npm install semantic-ui
then, I customised few variables per site, also few on element level, button for example.
This is all well, and I haven't had any problem changing those.
Then in theme.config file, I could specify:
#button: 'mytheme';
to pickup those custom style overrides
For my theme to be separate from Semantic UI core, I crated directory in the following path,
src/themes/mytheme
,and after running gulp build, I expected to have that mytheme exported over to dist/themes/mytheme along with basic and default which were already there. But that was not the case. So to be able to use my new button styles, I had to move manually newly generated button.css from dist/components after that gulp build task.
Am I doing this wrong? How to have all override files in one place like packaged theme? So I could then add <link> declaration after semantic.min.css to use my overrides.

What buttons have changed labels from fckeditor to ckeditor 4?

It appears that some of the button names have been changed in ckeditor version 4.
Is there a complete list of these changes?
There is a partial list here.
I'm pretty sure that names haven't been changed. If something is not working check if plugin you need is included in your build (most likely it is a standard preset) and if not:
download a full preset (which in fact does not include all plugins too, but most of them) or
add required plugins to your custom CKEditor build.
[EDIT] another useful resource is the toolbar sample shipped with CKEditor package. E.g. here's one for a standard package.

Adding tags to images in Wordpress

How can I add support for adding tags to images the same way you can add tags to posts in Wordpress?
I know I can do this by installing a plugin such as the Shiba media library plugin, but that adds a whole lot more than I need. I just need to be able to add tags, no more.
see this tutorial http://code.tutsplus.com/articles/applying-categories-tags-and-custom-taxonomies-to-media-attachments--wp-32319 that explains it all very well.
To use the same tags as your posts you'll just need to add those lines to your functions.php file:
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
After adding those, the tags box will start showing up on your media library items.
The article also describes how to create custom taxonomy (a separate set of tags / categories) but that's probably not required for what you're after.
You can use this plugin to add media tags :
http://wordpress.org/extend/plugins/wordpress-media-tags/
Or you can dig this solution :
https://wordpress.stackexchange.com/questions/29858/adding-category-tag-taxonomy-support-to-images-media
I found an easy solution.
Download the Shiba Media Library plugin at:
http://shibashake.com/wordpress-theme/media-library-plus-plugin
Install it.
Download the Admin Menu Editor plugin at:
http://wordpress.org/extend/plugins/admin-menu-editor/
install it.
With the Admin Menu Editor, hide the gallery menu item (added by the Shiba plugin).
Now I can tag images via the ordinary media library :)

Resources