WordPress: add nofollow and custom class to insert link box

The default WordPress insert link box is missing one important option: the ability to add rel=”nofollow”. In this post you’ll learn how to fix it. Plus how to add a custom class to insert link box.

You don’t want to have every outgoing link crawled by search engines. In such a case, to add rel=”nofollow” you’ll have to change view from Visual to Text, and insert the text between inside <a> tag. Not very convenient if you insert many links in a post, and don’t want most of them to be followed.

1. How to add rel=”nofollow” to insert link box

Fortunately there is a plugin, Rel Nofollow Checkbox, that just does that: adds the checkbox with rel=”nofollow” to insert link box, right under “Open link in a new window/tab”.

WordPress add nofollow to insert link box via plugin

This is a simple plugin that deregisters the default WordPress insert link function, and replaces it with almost the same code – modified with rel=”nofollow” command.

Simply add Rel Nofollow Checkbox plugin via Plugins section of the WordPress dashboard, and activate it. Done.

2. How to add rel=”nofollow” and custom class to insert link box

The ability to add custom class is useful if, for example, you style many links to look like buttons. Again, why change the view and insert the class manually, if you can have it as a checkbox in insert link box.

WordPress add nofollow and class to insert link box without plugin

If you’ll have all three checkboxes checked in the insert link box you’ll have the inside the <a> tag something like this:

Follow the steps below. To add custom class I’ve modified Rel Nofollow Checkbox plugin and moved it to theme folder. The plugin itself is no longer needed.

1. If you don’t have it yet, add a new folder to your current theme and name it scripts.

2. Inside scripts folder create a blank document, name it wplink.min.js, and paste the code from below (it’s already minified).

var wpLink;!function(e){var t,i,n,l,s={},a={};wpLink={timeToTriggerRiver:150,minRiverAJAXDuration:200,riverBottomThreshold:5,keySensitivity:100,lastSearch:"",textarea:"",append:function(){var t='
Add rel="nofollow"Add class="button"';e("#wp-link .link-target").append(t)},init:function(){s.wrap=e("#wp-link-wrap"),s.dialog=e("#wp-link"),s.backdrop=e("#wp-link-backdrop"),s.submit=e("#wp-link-submit"),s.close=e("#wp-link-close"),s.url=e("#url-field"),s.nonce=e("#_ajax_linking_nonce"),s.title=e("#link-title-field"),s.openInNewTab=e("#link-target-checkbox"),s.relNofollow=e("#rel-nofollow-checkbox"),s.myClass=e("#my-class-checkbox"),s.search=e("#search-field"),a.search=new n(e("#search-results")),a.recent=new n(e("#most-recent-results")),a.elements=s.dialog.find(".query-results"),s.dialog.keydown(wpLink.keydown),s.dialog.keyup(wpLink.keyup),s.submit.click(function(e){e.preventDefault(),wpLink.update()}),s.close.add(s.backdrop).add("#wp-link-cancel a").click(function(e){e.preventDefault(),wpLink.close()}),e("#wp-link-search-toggle").click(wpLink.toggleInternalLinking),a.elements.on("river-select",wpLink.updateFields),s.search.keyup(function(){var e=this;window.clearTimeout(i),i=window.setTimeout(function(){wpLink.searchInternalLinks.call(e)},500)})},open:function(i){var n;wpLink.range=null,i&&(window.wpActiveEditor=i),window.wpActiveEditor&&(this.textarea=e("#"+window.wpActiveEditor).get(0),"undefined"!=typeof tinymce&&(n=tinymce.get(wpActiveEditor),t=n&&!n.isHidden()?n:null,t&&tinymce.isIE&&(t.windowManager.bookmark=t.selection.getBookmark())),!wpLink.isMCE()&&document.selection&&(this.textarea.focus(),this.range=document.selection.createRange()),s.wrap.show(),s.backdrop.show(),wpLink.refresh())},isMCE:function(){return t&&!t.isHidden()},refresh:function(){a.search.refresh(),a.recent.refresh(),wpLink.isMCE()?wpLink.mceRefresh():wpLink.setDefaultValues(),s.url.focus()[0].select(),a.recent.ul.children().length||a.recent.ajax()},mceRefresh:function(){var e;(e=t.dom.getParent(t.selection.getNode(),"A"))?(s.url.val(t.dom.getAttrib(e,"href")),s.title.val(t.dom.getAttrib(e,"title")),s.openInNewTab.prop("checked","_blank"===t.dom.getAttrib(e,"target")),s.myClass.prop("checked","button"===t.dom.getAttrib(e,"class")),"nofollow"==ed.dom.getAttrib(e,"rel")&&s.relNofollow.prop("checked",!0),s.submit.val(wpLinkL10n.update)):wpLink.setDefaultValues()},close:function(){wpLink.isMCE()?t.focus():(wpLink.textarea.focus(),wpLink.range&&(wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select())),s.backdrop.hide(),s.wrap.hide()},getAttrs:function(){return{href:s.url.val(),title:s.title.val(),target:s.openInNewTab.prop("checked")?"_blank":"","class":s.myClass.prop("checked")?"button":"",rel:s.relNofollow.prop("checked")?"nofollow":""}},update:function(){wpLink.isMCE()?wpLink.mceUpdate():wpLink.htmlUpdate()},htmlUpdate:function(){var e,t,i,n,l,s,a,r=wpLink.textarea;r&&(e=wpLink.getAttrs(),e.href&&"http://"!=e.href&&(t='<a href="'+e.href+'"',e.title&&(s=e.title.replace(//g,">").replace(/"/g,"""),t+=' title="'+s+'"'),e.target&&(t+=' target="'+e.target+'"'),e.rel&&(t+=' rel="nofollow"'),e.class&&(t+=' class="button"'),t+=">",document.selection&&wpLink.range?(r.focus(),wpLink.range.text=t+wpLink.range.text+"
",wpLink.range.moveToBookmark(wpLink.range.getBookmark()),wpLink.range.select(),wpLink.range=null):"undefined"!=typeof r.selectionStart&&(i=r.selectionStart,n=r.selectionEnd,a=r.value.substring(i,n),t=t+a+"",l=i+t.length,i==n&&(l-="".length),r.value=r.value.substring(0,i)+t+r.value.substring(n,r.value.length),r.selectionStart=r.selectionEnd=l),wpLink.close(),r.focus()))},mceUpdate:function(){var e,i=wpLink.getAttrs();return wpLink.close(),t.focus(),tinymce.isIE&&t.selection.moveToBookmark(t.windowManager.bookmark),e=t.dom.getParent(t.selection.getNode(),"a[href]"),i.href&&"http://"!=i.href?(e?t.dom.setAttribs(e,i):t.execCommand("mceInsertLink",!1,i),void t.selection.collapse()):void t.execCommand("unlink")},updateFields:function(e,t,i){s.url.val(t.children(".item-permalink").val()),s.title.val(t.hasClass("no-title")?"":t.children(".item-title").text()),i&&"click"==i.type&&s.url.focus()},setDefaultValues:function(){s.url.val("http://"),s.title.val(""),s.submit.val(wpLinkL10n.save)},searchInternalLinks:function(){var t,i=e(this),n=i.val();if(n.length>2){if(a.recent.hide(),a.search.show(),wpLink.lastSearch==n)return;wpLink.lastSearch=n,t=i.parent().find(".spinner").show(),a.search.change(n),a.search.ajax(function(){t.hide()})}else a.search.hide(),a.recent.show()},next:function(){a.search.next(),a.recent.next()},prev:function(){a.search.prev(),a.recent.prev()},keydown:function(t){var i,n,l=e.ui.keyCode;l.ESCAPE===t.keyCode?(wpLink.close(),t.stopImmediatePropagation()):l.TAB===t.keyCode&&(n=t.target.id,"wp-link-submit"!==n||t.shiftKey?"wp-link-close"===n&&t.shiftKey&&(s.submit.focus(),t.preventDefault()):(s.close.focus(),t.preventDefault())),(t.keyCode===l.UP||t.keyCode===l.DOWN)&&(i=t.keyCode===l.UP?"prev":"next",clearInterval(wpLink.keyInterval),wpLink[i](),wpLink.keyInterval=setInterval(wpLink[i],wpLink.keySensitivity),t.preventDefault())},keyup:function(t){var i=e.ui.keyCode;(t.which===i.UP||t.which===i.DOWN)&&(clearInterval(wpLink.keyInterval),t.preventDefault())},delayedCallback:function(e,t){var i,n,l,s;return t?(setTimeout(function(){return n?e.apply(s,l):void(i=!0)},t),function(){return i?e.apply(this,arguments):(l=arguments,s=this,void(n=!0))}):e},toggleInternalLinking:function(){var e=s.wrap.hasClass("search-panel-visible");s.wrap.toggleClass("search-panel-visible",!e),setUserSetting("wplink",e?"0":"1"),s[e?"url":"search"].focus()}},n=function(t,i){var n=this;this.element=t,this.ul=t.children("ul"),this.contentHeight=t.children("#link-selector-height"),this.waiting=t.find(".river-waiting"),this.change(i),this.refresh(),e("#wp-link .query-results, #wp-link #link-selector").scroll(function(){n.maybeLoad()}),t.on("click","li",function(t){n.select(e(this),t)})},e.extend(n.prototype,{refresh:function(){this.deselect(),this.visible=this.element.is(":visible")},show:function(){this.visible||(this.deselect(),this.element.show(),this.visible=!0)},hide:function(){this.element.hide(),this.visible=!1},select:function(e,t){var i,n,l,s;e.hasClass("unselectable")||e==this.selected||(this.deselect(),this.selected=e.addClass("selected"),i=e.outerHeight(),n=this.element.height(),l=e.position().top,s=this.element.scrollTop(),0>l?this.element.scrollTop(s+l):l+i>n&&this.element.scrollTop(s+l-n+i),this.element.trigger("river-select",[e,t,this]))},deselect:function(){this.selected&&this.selected.removeClass("selected"),this.selected=!1},prev:function(){if(this.visible){var e;this.selected&&(e=this.selected.prev("li"),e.length&&this.select(e))}},next:function(){if(this.visible){var t=this.selected?this.selected.next("li"):e("li:not(.unselectable):first",this.element);t.length&&this.select(t)}},ajax:function(e){var t=this,i=1==this.query.page?0:wpLink.minRiverAJAXDuration,n=wpLink.delayedCallback(function(i,n){t.process(i,n),e&&e(i,n)},i);this.query.ajax(n)},change:function(e){this.query&&this._search==e||(this._search=e,this.query=new l(e),this.element.scrollTop(0))},process:function(t,i){var n="",l=!0,s="",a=1==i.page;t?e.each(t,function(){s=l?"alternate":"",s+=this.title?"":" no-title",n+=s?'
  • ':"
  • ",n+='',n+='',n+=this.title?this.title:wpLinkL10n.noTitle,n+=''+this.info+"
  • ",l=!l}):a&&(n+='
  • '+wpLinkL10n.noMatchesFound+"
  • "),this.ul[a?"html":"append"](n)},maybeLoad:function(){var e=this,t=this.element,i=t.scrollTop()+t.height();!this.query.ready()||i<this.contentHeight.height()-wpLink.riverBottomThreshold||setTimeout(function(){var i=t.scrollTop(),n=i+t.height();!e.query.ready()||n<e.contentHeight.height()-wpLink.riverBottomThreshold||(e.waiting.show(),t.scrollTop(i+e.waiting.outerHeight()),e.ajax(function(){e.waiting.hide()}))},wpLink.timeToTriggerRiver)}}),l=function(e){this.page=1,this.allLoaded=!1,this.querying=!1,this.search=e},e.extend(l.prototype,{ready:function(){return!(this.querying||this.allLoaded)},ajax:function(t){var i=this,n={action:"wp-link-ajax",page:this.page,_ajax_linking_nonce:s.nonce.val()};this.search&&(n.search=this.search),this.querying=!0,e.post(ajaxurl,n,function(e){i.page++,i.querying=!1,i.allLoaded=!e,t(e,n)},"json")}}),e(document).ready(wpLink.append),e(document).ready(wpLink.init)}(jQuery);

    As you’ve seen in the example above, a default class to be inserted is “button”. You can change it to whatever you want.

    Open wplink.min.js document, and use Find function to replace every instance of the word “button” with the desired class name.

    More posts on WordPress:

    [pi-archive number=5 tag=”wordpress”]

    Leave a Reply

    Please log in using one of these methods to post your comment:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s

    %d bloggers like this: