Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #81 from WordPress/add-wp-link-attribute
Browse files Browse the repository at this point in the history
🎨 Add wp-link attribute to all links
  • Loading branch information
SantosGuillamot authored Oct 17, 2022
2 parents c17ce2b + ac36ece commit 67aa9f5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions wp-directives.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Plugin Name: wp-directives
* Version: 0.1.0
Expand Down Expand Up @@ -36,3 +37,22 @@ function wp_directives_register_scripts()
}

add_action('init', 'wp_directives_register_scripts');

function add_wp_link_attribute($block_content)
{
$site_url = parse_url(get_site_url());
$w = new WP_HTML_Tag_Processor($block_content);
while ($w->next_tag('a')) {
if ($w->get_attribute('target') === '_blank') {
break;
}

$link = parse_url($w->get_attribute('href'));
if (is_null($link['host']) || ($link['host'] === $site_url['host'])) {
$w->set_attribute('wp-link', 'true');
}
};
return (string) $w;
}

add_filter('render_block', 'add_wp_link_attribute', 10, 2);

0 comments on commit 67aa9f5

Please sign in to comment.