Last active
December 7, 2020 22:01
-
-
Save xavier-bs/ebec023da0b827d3e74df39056af6589 to your computer and use it in GitHub Desktop.
Fonctionality to alter wplink plugin. Adds new checkboxes to define rel attribute in <a> tag with noopener, noreferer, sponsored, nofollow and/or external values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fonctionality to alter wplink plugin, | |
* Adds new checkboxes to define rel attribute in <a> tag with noopener, noreferer, sponsored, nofollow and/or external values. | |
* Enqueue with admin_enqueue_scripts on post.php and post-new.php | |
* wp_enqueue_script( 'post-wplink', "/url/to/post-wplink.js", ['wplink'], $version, true ); | |
* wlink-open event occurs when Insert/edit link modal box opens | |
* wpLink.buildHtml function replaces the function from wplink.js | |
* The string translate.mark_as_string might be define with localize_script(), contains __( 'Mark as <code>%s</code>', $textdomain ); | |
* Values that may be added to the rel attribute are defined in the rels array. | |
* We push down the search terms div of shift=140 px and expand the modal height to 600 px. You may set these values to whatever you want. | |
* Xavier Birnie-Scott | |
* 2020-12-07 | |
*/ | |
($ => { | |
const rels = ['noopener', 'noreferer', 'sponsored', 'nofollow', 'external']; | |
const mark_as = translate.mark_as_string; | |
const shift = 140; | |
const modal_height = 600; | |
$(document).on('wplink-open', wrap => { | |
// Sets the height of the modal | |
$('#wp-link-wrap').height(modal_height); | |
// Adds 4 checkboxes | |
if (! $('[name="noopener"]').length) { | |
// Appends checkboxes | |
const $linkOptions = $('#link-options'); | |
rels.forEach(el => { | |
const label = mark_as.replace(/%s/, el); | |
$linkOptions.append('\ | |
<div><label><span></span>\ | |
<input type="checkbox" name="' + el + '"/> ' + label + '\ | |
</label></div>'); | |
}); | |
// Position of #most-recent-results div | |
const $mostRecentResults = $('#most-recent-results'); | |
$mostRecentResults.css('top', parseInt($mostRecentResults.css('top')) + shift + 'px'); | |
} | |
}); | |
// Overrides wpLink.getAttrs function from wplink.js | |
window.wpLink.getAttrs = () => { | |
wpLink.correctURL(); | |
var relation = ''; | |
rels.forEach(rel => { | |
relation += $('[name="' + rel + '"]').prop('checked') ? `${rel} ` : ''; | |
}); | |
relation = relation.trim(); | |
return { | |
href: $.trim($('#wp-link-url').val()), | |
target: $('#wp-link-target').prop('checked') ? '_blank' : null, | |
rel: relation.length ? relation : null | |
}; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since 4.7.4, wordpress adds
noopener noreferrer
on links that have atarget
attribute. When the post is saved, missing values are added to therel
attribute if thetarget
attribute exists. This is done inwp-includes/formatting.php
. To change this behavior, use thewp_targeted_link_rel
filter to change the string'noopener noreferrer'
.