Using WordPress ‘comment_max_links_url’ PHP filter

The comment_max_links_url WordPress PHP filter allows you to control the maximum number of links allowed in a comment.

Usage

add_filter('comment_max_links_url', 'your_custom_function', 10, 3);

function your_custom_function($num_links, $url, $comment) {
  // your custom code here
  return $num_links;
}

Parameters

  • $num_links (int): The number of links found.
  • $url (string): Comment author’s URL. Included in allowed links total.
  • $comment (string): Content of the comment.

More information

See WordPress Developer Resources: comment_max_links_url

Examples

To increase the maximum allowed links in comments to 5, use the following code:

add_filter('comment_max_links_url', 'increase_max_links', 10, 3);

function increase_max_links($num_links, $url, $comment) {
  $num_links = 5;
  return $num_links;
}

Limit comments to only 1 link with the following code:

add_filter('comment_max_links_url', 'limit_to_one_link', 10, 3);

function limit_to_one_link($num_links, $url, $comment) {
  $num_links = 1;
  return $num_links;
}

Count the author URL as a link and limit the total links to 3:

add_filter('comment_max_links_url', 'count_author_url', 10, 3);

function count_author_url($num_links, $url, $comment) {
  if (!empty($url)) {
    $num_links = 2;
  } else {
    $num_links = 3;
  }
  return $num_links;
}

Disallow any links in comments:

add_filter('comment_max_links_url', 'no_links_allowed', 10, 3);

function no_links_allowed($num_links, $url, $comment) {
  $num_links = 0;
  return $num_links;
}

Different limits for different user roles

Set different limits for different user roles:

add_filter('comment_max_links_url', 'user_role_based_limits', 10, 3);

function user_role_based_limits($num_links, $url, $comment) {
  $user = wp_get_current_user();

  if (in_array('administrator', $user->roles)) {
    $num_links = 10;
  } elseif (in_array('editor', $user->roles)) {
    $num_links = 5;
  } else {
    $num_links = 2;
  }

  return $num_links;
}