The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Automatically Add a Custom Excerpt to Posts Without One

Shared by Amit Dholiya · November 23, 2026 · @get_the_excerpt

5 views
1 upvote
Back to Snippets

Steps

  1. Add the PHP code to your theme’s functions.php file or a PHP snippets plugin.

  2. Save and activate the snippet.

  3. Open a blog post without a manually written excerpt.

  4. WordPress will generate an excerpt from its content.

/**
 * Generate an excerpt when a post has no manual excerpt
 */
function custom_generate_default_excerpt( $excerpt, $post ) {

    if ( ! empty( $excerpt ) || 'post' !== $post->post_type ) {
        return $excerpt;
    }

    $content = strip_shortcodes( $post->post_content );
    $content = wp_strip_all_tags( $content );

    return wp_trim_words( $content, 25, '...' );
}

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

Tested on

WordPress 6.8

Reported by the author on November 23, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.5. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by