Skip to main content
added 32 characters in body
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt and the_excerpt, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post — below is the relevant code in wp-includes/default-filters.php:

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

So if you want to access the auto-generated excerpt, then you can usewith the the_excerpt hook, you can do it like so:

add_filter( 'the_excerpt', function ( $excerpt ) {
    return has_excerpt() ?
        'Has custom excerpt: ' . $excerpt :
        'Automatic excerpt: ' . $excerpt;
} );

But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.

And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automattic""automatic" excerpt:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );

add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
    return $excerpt$post->post_excerpt ?
        'Has custom excerpt: ' . $excerpt :
        'Here, create your own excerpt.';
}, 10, 2 );

The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt and the_excerpt, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post — below is the relevant code in wp-includes/default-filters.php:

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

So if you want to access the auto-generated excerpt, then you can use the the_excerpt hook like so:

add_filter( 'the_excerpt', function ( $excerpt ) {
    return has_excerpt() ?
        'Has custom excerpt: ' . $excerpt :
        'Automatic excerpt: ' . $excerpt;
} );

But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.

And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automattic" excerpt:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );

add_filter( 'get_the_excerpt', function ( $excerpt ) {
    return $excerpt ?
        'Has custom excerpt: ' . $excerpt :
        'Here, create your own excerpt.';
} );

The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt and the_excerpt, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post — below is the relevant code in wp-includes/default-filters.php:

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

So if you want to access the auto-generated excerpt, then with the the_excerpt hook, you can do it like so:

add_filter( 'the_excerpt', function ( $excerpt ) {
    return has_excerpt() ?
        'Has custom excerpt: ' . $excerpt :
        'Automatic excerpt: ' . $excerpt;
} );

But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.

And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automatic" excerpt:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );

add_filter( 'get_the_excerpt', function ( $excerpt, $post ) {
    return $post->post_excerpt ?
        'Has custom excerpt: ' . $excerpt :
        'Here, create your own excerpt.';
}, 10, 2 );
Better answer
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

If aThe proper hooks for modifying the post doesn't have a custom or manually-entered excerpt are the ones you already tried: get_the_excerpt and the_excerpt, and WordPress actually uses wp_trim_excerpt()the former one to generate an excerpt from the full post content, and that function has a filter withif there's no custom or manually-specified excerpt for the same name as that functionpost below is the relevant code in wp_trim_excerpt — whichwp-includes/default-filters.php:

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

So if you can usewant to access the automaticauto-generated excerpt. So for example, then you can use the the_excerpt hook like so:

add_filter( 'wp_trim_excerpt''the_excerpt', function ( $excerpt ) {
    //return Dohas_excerpt() your?
 thing here      'Has custom excerpt: ' . $excerpt :
        'Automatic excerpt: ' . $excerpt;
} );

But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.

And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automattic" excerpt:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );

add_filter( 'get_the_excerpt', function ( $excerpt ) {
    return $excerpt;$excerpt ?
        'Has custom excerpt: ' . $excerpt :
        'Here, create your own excerpt.';
} );

If a post doesn't have a custom or manually-entered excerpt, WordPress uses wp_trim_excerpt() to generate an excerpt from the full content, and that function has a filter with the same name as that functionwp_trim_excerpt — which you can use to access the automatic excerpt. So for example:

add_filter( 'wp_trim_excerpt', function ( $excerpt ) {
    // Do your thing here.

    return $excerpt;
} );

The proper hooks for modifying the post excerpt are the ones you already tried: get_the_excerpt and the_excerpt, and WordPress actually uses the former one to generate an excerpt from the full post content, if there's no custom or manually-specified excerpt for the post below is the relevant code in wp-includes/default-filters.php:

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

So if you want to access the auto-generated excerpt, then you can use the the_excerpt hook like so:

add_filter( 'the_excerpt', function ( $excerpt ) {
    return has_excerpt() ?
        'Has custom excerpt: ' . $excerpt :
        'Automatic excerpt: ' . $excerpt;
} );

But take note, the automatic excerpt may not necessarily be the one initially generated by WordPress and WordPress might not even be the one that generated the excerpt — plugins could have completely overridden it or just customized it, just as you can do the same.

And as you may have guessed it, you can remove the default WordPress filter and then use your own callback to generate your own "automattic" excerpt:

remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10, 2 );

add_filter( 'get_the_excerpt', function ( $excerpt ) {
    return $excerpt ?
        'Has custom excerpt: ' . $excerpt :
        'Here, create your own excerpt.';
} );
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

If a post doesn't have a custom or manually-entered excerpt, WordPress uses wp_trim_excerpt() to generate an excerpt from the full content, and that function has a filter with the same name as that function — wp_trim_excerpt — which you can use to access the automatic excerpt. So for example:

add_filter( 'wp_trim_excerpt', function ( $excerpt ) {
    // Do your thing here.

    return $excerpt;
} );