Skip to content

Instantly share code, notes, and snippets.

@xavier-bs
Last active December 7, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavier-bs/ebec023da0b827d3e74df39056af6589 to your computer and use it in GitHub Desktop.
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
/**
* 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);
@xavier-bs
Copy link
Author

xavier-bs commented Dec 7, 2020

Since 4.7.4, wordpress adds noopener noreferrer on links that have a target attribute. When the post is saved, missing values are added to the rel attribute if the target attribute exists. This is done in wp-includes/formatting.php. To change this behavior, use the wp_targeted_link_rel filter to change the string 'noopener noreferrer'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment