Dofs
Dofs

Reputation: 19197

Nofollow on links in CKEditor

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?

Upvotes: 2

Views: 3392

Answers (4)

user7057468
user7057468

Reputation: 15

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;
}

Upvotes: 0

Vijay Chouhan
Vijay Chouhan

Reputation: 4883

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';
              }
          }
      });
    })

Upvotes: 0

Sergey
Sergey

Reputation: 11

Need wrapper:

editor.on('instanceReady',function(){...})

Upvotes: 0

AlfonsoML
AlfonsoML

Reputation: 12690

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';
        }
    }
});

Upvotes: 8

Related Questions