# Rank Math Description Fallback From the Excerpt

> Fills an empty Rank Math meta description on posts and pages with the excerpt or opening text, trimmed to about 155 characters.

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

## Code

```php
// Maximum description length.
$wpide_rm_desc_length = 155;

add_filter( 'rank_math/frontend/description', function ( $description ) use ( $wpide_rm_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_rm_desc_length ) {
        $text = rtrim( mb_substr( $text, 0, $wpide_rm_desc_length - 1 ), " ,.;:-" ) . '…';
    }

    return $text;
} );
```

## Notes

Descriptions you write in Rank Math always win.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Rank Math Description Fallback From the Excerpt" 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/
