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

🎨 Add wp-link attribute to all links #81

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 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,23 @@ 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')) {
$link = parse_url($w->get_attribute('href'));

if ($w->get_attribute('target') === '_blank') {
break;
}

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

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