# Use the Excerpt When a Yoast Meta Description Is Empty

> Fills an empty Yoast SEO meta description with the post's excerpt (or its opening words), trimmed to a search-friendly length.

- Type: PHP
- Runs: Frontend only
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: yoast, meta-description, excerpt
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/yoast-metadesc-excerpt-fallback/

## Code

```php
// Maximum description length in characters.
$wpide_desc_length = 155;

add_filter( 'wpseo_metadesc', function ( $description ) use ( $wpide_desc_length ) {
    if ( '' !== trim( (string) $description ) || ! is_singular() ) {
        return $description;
    }

    $post = get_queried_object();

    if ( ! $post instanceof WP_Post ) {
        return $description;
    }

    $text = has_excerpt( $post ) ? $post->post_excerpt : strip_shortcodes( $post->post_content );
    $text = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( $text ) ) );

    if ( mb_strlen( $text ) > $wpide_desc_length ) {
        $text = rtrim( mb_substr( $text, 0, $wpide_desc_length - 1 ), " ,.;:-" ) . '…';
    }

    return $text;
} );
```

## Notes

Descriptions set in Yoast, and Yoast's template descriptions, always win.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use the Excerpt When a Yoast Meta Description Is Empty" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
