Using WordPress ‘make_clickable_rel’ PHP filter

The make_clickable_rel WordPress PHP Filter allows you to modify the ‘rel’ attribute value of automatically created links in WordPress.

Usage

add_filter('make_clickable_rel', 'your_custom_function', 10, 2);
function your_custom_function($rel, $url) {
  // your custom code here
  return $rel;
}

Parameters

  • $rel (string) – The current ‘rel’ attribute value for the link.
  • $url (string) – The URL being matched and converted into a link tag.

More information

See WordPress Developer Resources: make_clickable_rel

Examples

Add ‘nofollow’ to the ‘rel’ attribute of all auto-generated links.

add_filter('make_clickable_rel', 'add_nofollow_to_links', 10, 2);
function add_nofollow_to_links($rel, $url) {
  $rel = 'nofollow';
  return $rel;
}

Add ‘noopener’ to the ‘rel’ attribute of external links only.

add_filter('make_clickable_rel', 'add_noopener_to_external_links', 10, 2);
function add_noopener_to_external_links($rel, $url) {
  if (strpos($url, home_url()) === false) {
    $rel = 'noopener';
  }
  return $rel;
}

Remove All ‘rel’ Attributes

Remove the ‘rel’ attribute from all auto-generated links.

add_filter('make_clickable_rel', 'remove_rel_attribute', 10, 2);
function remove_rel_attribute($rel, $url) {
  $rel = '';
  return $rel;
}

Add ‘sponsored’ to the ‘rel’ attribute for links to a specific domain.

add_filter('make_clickable_rel', 'add_sponsored_to_specific_domain', 10, 2);
function add_sponsored_to_specific_domain($rel, $url) {
  if (strpos($url, 'example.com') !== false) {
    $rel = 'sponsored';
  }
  return $rel;
}

Custom ‘rel’ Attribute Based on URL Pattern

Add a custom ‘rel’ attribute value based on a specific URL pattern.

add_filter('make_clickable_rel', 'custom_rel_based_on_url_pattern', 10, 2);
function custom_rel_based_on_url_pattern($rel, $url) {
  if (strpos($url, 'download') !== false) {
    $rel = 'download';
  }
  return $rel;
}