Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:56+00:00 2026-05-26T01:20:56+00:00

I am writing a CKEditor plugin to apply a specific class to an element.

  • 0

I am writing a CKEditor plugin to apply a specific class to an element. Basically this class
is setting the text colour to a specific redish colour.

Anyways, I am not getting how to define a class for the wrapped text.

Please look at my plugin code:

CKEDITOR.plugins.add( 'red',
{
    init: function( editor )
    {
        editor.addCommand( 'red',
            {
                exec : function( editor )
                {    
                    var format = {
                        element : 'span'
                    };

                    var style = new CKEDITOR.style(format);
                    style.apply(editor.document);
                }
            });
        editor.ui.addButton( 'red',
        {
            label: 'red',
            command: 'red',
            icon: this.path + 'images/red.png'
        } );
    }
} );

Basically I want to have an output like:

<span class="red">This is now a red text</span>

Thank you very much in advance for helping me.

The sources I have used to get this far:
http://docs.cksource.com/CKEditor_3.x/Howto
http://docs.cksource.com/CKEditor_3.x/Tutorials/Timestamp_Plugin
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.command.html#exec

Maybe I overread something there, but for me it does not seem that this kind of things are mentioned there ? Please proof me wrong : )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:20:58+00:00Added an answer on May 26, 2026 at 1:20 am

    You can use the the “basicstyles” plugin as a template, it creates the various style buttons (bold, italic, etc):
    ckeditor/_source/plugins/basicstyles/plugin.js

    Here’s the code for your plugin (based on the basicstyles plugin), it would be the contents of the plugin.js file located here:
    ckeditor/plugins/red/plugin.js

    The icon for the button would be located here:
    ckeditor/plugins/red/images

    CKEDITOR.plugins.add( 'red',
    {
      requires : [ 'styles', 'button' ],
    
      init : function( editor )
      {
        // This "addButtonCommand" function isn't needed, but
        // would be useful if you want to add multiple buttons
        var addButtonCommand = function( buttonName, buttonLabel, commandName, styleDefiniton )
        {
          var style = new CKEDITOR.style( styleDefiniton );
          editor.attachStyleStateChange( style, function( state )
            {
              !editor.readOnly && editor.getCommand( commandName ).setState( state );
            });
    
          editor.addCommand( commandName, new CKEDITOR.styleCommand( style ) );
          editor.ui.addButton( buttonName,
            {
              label : buttonLabel,
              command : commandName,
              icon: CKEDITOR.plugins.getPath('red') + 'images/red.png'
            });
        };
    
        var config = editor.config,
          lang = editor.lang;
    
        // This version uses the language functionality, as used in "basicstyles"
        // you'll need to add the label to each language definition file
        addButtonCommand( 'Red'   , lang.red    , 'red'   , config.coreStyles_red );
    
        // This version hard codes the label for the button by replacing `lang.red` with 'Red'
        addButtonCommand( 'Red'   , 'Red'   , 'red'   , config.coreStyles_red );
      }
    });
    
    // The basic configuration that you requested
    CKEDITOR.config.coreStyles_red = { element : 'span', attributes : {'class': 'red'} };
    
    // You can assign multiple attributes too
    CKEDITOR.config.coreStyles_red = { element : 'span', attributes : { 'class': 'red', 'style' : 'background-color: yellow;', 'title' : 'Custom Format Entry' } };
    

    Add the style for the “red” class to the ckeditor contents.css file in addition to your regular style sheet (unless you load the regular sheet as a custom css file in ckeditor).

    Add the new plugin in your config file:

    config.extraPlugins = 'red';
    

    And add the button to your toolbar 'Red'

    For the label, you could create an array with different labels assigned to each language code and pull the correct version based on the active language code. Use editor.langCode to get the active language code.

    If you’d like to avoid adding a button, you could add a new entry to the “Format” Selector instead. It’s the selector with the headings.

    Be Well,
    Joe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a custom dialog/plugin for ckeditor . What I want to know is
I'm new to CKEditor , writing my first plugin and i just can't find
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing a python program, and I came up with this error while using the
I'm writing a Chrome userscript to locally auto-save content in a CKEditor . I'm
Writing to a file is not working after hosting the web application, whereas they
Writing a shell script and I want to do something like this: cp myfile.ext
Writing a class, how do I implement foo.send(item) ? __iter__ allows iterating over the
writing to a text file is working good...But my problem is text file is
Writing my first PHP Class and came across an issue, the following is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.