Using WordPress ‘comment_excerpt_length’ PHP filter

The comment_excerpt_length WordPress PHP filter allows you to change the maximum number of words displayed in the comment excerpt.

Usage

add_filter('comment_excerpt_length', 'your_custom_function_name');
function your_custom_function_name($comment_excerpt_length) {
  // your custom code here

  return $comment_excerpt_length;
}

Parameters

  • $comment_excerpt_length (int): The number of words you want to display in the comment excerpt.

More information

See WordPress Developer Resources: comment_excerpt_length

Examples

Shorten comment excerpts to 10 words

This example limits comment excerpts to 10 words.

add_filter('comment_excerpt_length', 'shorten_comment_excerpt');
function shorten_comment_excerpt($length) {
  return 10;
}

Increase comment excerpts to 50 words

This example increases the comment excerpt length to 50 words.

add_filter('comment_excerpt_length', 'increase_comment_excerpt');
function increase_comment_excerpt($length) {
  return 50;
}

Set comment excerpt length based on user role

This example sets different comment excerpt lengths based on the user’s role.

add_filter('comment_excerpt_length', 'set_comment_excerpt_by_role');
function set_comment_excerpt_by_role($length) {
  if (current_user_can('administrator')) {
    return 100;
  } else {
    return 25;
  }
}

Disable comment excerpts

This example disables comment excerpts by setting the length to 0.

add_filter('comment_excerpt_length', 'disable_comment_excerpt');
function disable_comment_excerpt($length) {
  return 0;
}