Using WordPress ‘get_the_excerpt’ PHP filter

The get_the_excerpt WordPress PHP filter allows you to modify the post excerpt after it is retrieved from the database and before it is returned from the get_the_excerpt() function.

Usage

function your_custom_function( $excerpt, $post ) {
    // your custom code here
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'your_custom_function', 10, 2 );

Parameters

  • $post_excerpt (string) – The post excerpt.
  • $post (WP_Post) – Post object.

More information

See WordPress Developer Resources: get_the_excerpt

Examples

Prevent Automatic Excerpts in WP REST API

Disable automatic excerpt generation from the_content via following filter hook.

remove_filter('get_the_excerpt', 'wp_trim_excerpt');

Limit Excerpt Length

Limit the post excerpt to 30 characters.

function limit_excerpt_length( $excerpt ) {
    if ( strlen( $excerpt ) > 30 ) {
        $excerpt = substr( $excerpt, 0, 30 ) . '...';
    }
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'limit_excerpt_length' );

Add a “Read More” link at the end of the post excerpt.

function add_read_more_link( $excerpt, $post ) {
    $read_more_link = ' <a href="' . get_permalink( $post ) . '">Read More</a>';
    return $excerpt . $read_more_link;
}
add_filter( 'get_the_excerpt', 'add_read_more_link', 10, 2 );

Display Custom Message for Empty Excerpts

Display “No excerpt available” when the post excerpt is empty.

function custom_empty_excerpt_message( $excerpt ) {
    if ( empty( $excerpt ) ) {
        $excerpt = 'No excerpt available.';
    }
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'custom_empty_excerpt_message' );

Remove Shortcodes from Excerpt

Remove all shortcodes from the post excerpt.

function remove_shortcodes_from_excerpt( $excerpt ) {
    $excerpt = strip_shortcodes( $excerpt );
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'remove_shortcodes_from_excerpt' );

Capitalize Excerpt

Capitalize the first letter of the post excerpt.

function capitalize_excerpt( $excerpt ) {
    $excerpt = ucfirst( $excerpt );
    return $excerpt;
}
add_filter( 'get_the_excerpt', 'capitalize_excerpt' );

Limit the length of the excerpt

This example limits the length of the excerpt to 20 words.

add_filter('get_the_excerpt', 'limit_excerpt_length', 10, 2);

function limit_excerpt_length($post_excerpt, $post) {
  $words = explode(' ', $post_excerpt);
  return implode(' ', array_slice($words, 0, 20)) . '...';
}

This example adds a “Read more” link to the end of the excerpt.

add_filter('get_the_excerpt', 'add_read_more_link', 10, 2);

function add_read_more_link($post_excerpt, $post) {
  $read_more_link = sprintf('<a href="%s">Read more</a>', get_permalink($post));
  return $post_excerpt . ' ' . $read_more_link;
}

Remove shortcodes from the excerpt

This example removes any shortcodes from the excerpt.

add_filter('get_the_excerpt', 'remove_shortcodes_from_excerpt', 10, 2);

function remove_shortcodes_from_excerpt($post_excerpt, $post) {
  return strip_shortcodes($post_excerpt);
}

Add a custom prefix to the excerpt

This example adds a custom prefix “Summary: ” to the excerpt.

add_filter('get_the_excerpt', 'add_custom_prefix_to_excerpt', 10, 2);

function add_custom_prefix_to_excerpt($post_excerpt, $post) {
  return 'Summary: ' . $post_excerpt;
}

Modify the excerpt based on post category

This example adds a custom prefix to the excerpt based on the post category.

add_filter('get_the_excerpt', 'modify_excerpt_based_on_category', 10, 2);

function modify_excerpt_based_on_category($post_excerpt, $post) {
  if (in_category('news', $post)) {
    return 'News: ' . $post_excerpt;
  } elseif (in_category('events', $post)) {
    return 'Event: ' . $post_excerpt;
  }
  return $post_excerpt;
}