Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a link into CKEditor

Tags:

ckeditor

I'm trying to insert a link into an instance of CKEditor using the following line: -

CKEDITOR.instances.CKInstance.insertHtml('<a href="http://www.example.com">My Text</a>');

All that happens though is that 'MyText' is inserted without the link. Anyone know how to insert the link properly?

PS. I know that CKEditor comes with a plugin to insert links but I'm doing my own one

Thanks Shazoo

like image 568
Shazoo Avatar asked Aug 14 '26 14:08

Shazoo


1 Answers

I guess that you're using CKEditor 4.1 or newer. And since you don't use the official link plugin, your editor discards all <a> tags. You need to properly configure Allowed Content Filter so your editor accepts <a> tags back again.

You can do it when defining your command, like this:

// Assuming you want a dialog-driven command...
editor.addCommand( 'yourCommand', new CKEDITOR.dialogCommand( 'link', {
    allowedContent: 'a[!href]', // Allow <a> in the editor with mandatory href attr.
    requiredContent: 'a[href]' // This command requires <a> with href to be enabled.
} ) );

Or in editor's config with config.extraAllowedContent = 'a[!href]'. This is not recommended though as you develop a plugin (right?), which should bring a custom command.

like image 142
oleq Avatar answered Aug 16 '26 16:08

oleq