0

WordPress 5.2 destroys structured data by adding rel="noopener noreferrer" to widget output (also to shortcodes): How to display the shortcode output without code change?

This feature was added in 5.2: https://core.trac.wordpress.org/ticket/43280

Basically there is nothing wrong with adding rel="noopener noreferrer" to <a href="..." target="_blank"></a> links, as it will protect from malicious comments or bad authors for instance.

Anyway I have a shortcode from some plugin which is generating structured data (rating stars) for a website and embedded via shortcode. Adding the rel-Attribute there breaks the structured data (as confirmed by https://search.google.com/structured-data/testing-tool/u/0/?hl=de).

How can I use the shortcode in a sidebar widget with the code preserved?

1

Try this code:

add_filter( 'wp_targeted_link_rel', '__return_false', 9999 );

function widget_text_replace($text) {
    $search = array('rel="noopener"');
    $replace = array('');
    $text = str_replace($search, $replace, $text);
    return $text;
}
add_filter('widget_text', 'widget_text_replace');
  • Upvote for this nice reverse hack filter. Even better would be a solution which prevents the altering. Because after some update the WordPress core may change the code to some different HTML and then you always have to adjust this. – Blackbam May 16 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.