44054

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?

Answer1:

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

Answer2:

Need wrapper:

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

Answer3:

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

Answer4:

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

Recommend

  • How to append code to parent template from included template
  • OleDB selecting multiple CSVs with first row as field names in C#
  • Unable to compute a CMAC using Cygwin and OpenSSL
  • angular js '?' in ng-src causes infinite loop
  • Unix:Recursively unzipping .zip file in respective folder
  • Better way to move MongoDB Collection to another Collection
  • How to list languages supported by my application
  • Find out specific weblogic process running
  • Is there any way to convert json to xml in PHP?
  • gridview on page won't refresh, even when calling databind again
  • How to prevent Console Application from terminating when pressing “Ctrl+C” in C?
  • git-svn: automatically importing/creating svn revisions as git tags
  • DataGrid show an empty row when DataTable is empty
  • How to find the start and end date of a previous month in PHP [duplicate]
  • How to sort a javascript array by date
  • ASP.NET Gridview Paging Problem
  • How do I embed a custom user-control in MS Word?
  • gulp.watch running same task multiple times when saving many files
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • CKeditor stripping font tags instead of converting to span
  • How can I add div content to a new page in jspdf?
  • how to get data attributes of dynamically generated element
  • ListItem.Attributes.Add not working
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • How to change the font size of a single index for UISegmentedControl?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • Weird JavaScript statement, what does it mean?
  • Unanticipated behavior
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?