Does anyone know where in CKEditor I can setup, so all links added will have rel="nofollow", even if the users don't specify it?
Asked
Active
Viewed 3,185 times
4 Answers
8
You can create a data filter as explained in this page that checks every link: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Data_Processor
This (untested) code should be more or less what you need:
editor.dataProcessor.htmlFilter.addRules(
{
elements :
{
a : function( element )
{
if ( !element.attributes.rel )
element.attributes.rel = 'nofollow';
}
}
});

AlfonsoML
- 12,634
- 2
- 46
- 53
-
Thanks, this was excately what I was looking for. – Dofs Aug 03 '11 at 18:07
-
3You should add this code to body of instanceReady event. `CKEDITOR.on('instanceReady', function( ev ) { .... });` – hkulekci Oct 15 '13 at 22:10
-
How can I add rel="nofollow" only *external links*? @AlfonsoML – hakki Sep 09 '16 at 17:24
-
I see that you have already created a separate question and it has the correct answer http://stackoverflow.com/questions/39416655/how-can-i-add-rel-nofollow-to-a-link-in-ckeditor-if-its-an-external-link – AlfonsoML Sep 09 '16 at 21:05
0
Put the code on page where ckeditor is loading
CKEDITOR.on('dialogDefinition', function(ev) {
var editor = ev.editor;
editor.dataProcessor.htmlFilter.addRules(
{
elements :
{
a : function( element )
{
if ( !element.attributes.rel )
element.attributes.rel = 'nofollow';
}
}
});
})

Vijay Chouhan
- 4,613
- 5
- 29
- 35
0
I'm using PHP Codeigniter with CKEditor and if you want to add rel="nofollow" only for external links you can modify the output of CKEditor before save it to database. Here the PHP function I use to modify:
function addNofollow($content) {
$dom = new DOMDocument();
@$dom -> loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
$x = new DOMXPath($dom);
// Add rel="nofollow"
foreach ($x -> query("//a") as $node) {
$href = $node -> getAttribute("href");
if (!strpos($href, site_base())) {
$node -> setAttribute("rel","nofollow");
} else {
$node -> removeAttribute("rel");
}
}
// Remove <script> tag
$script = $dom->getElementsByTagName('script');
$remove = [];
foreach ($script as $item) {
$remove[] = $item;
}
foreach ($remove as $item) {
$item -> parentNode -> removeChild($item);
}
$newHtml = $dom -> saveHtml($dom->getElementsByTagName('div')->item(0));
return $newHtml;
}

user7057468
- 15
- 1
- 4