# Automatic Meta Description

> Prints a meta description from the excerpt or opening words of posts, from term descriptions on archives and from the tagline on the homepage. Skipped when an SEO plugin is active.

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

## Code

```php
add_action( 'wp_head', function () {
    if ( defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'AIOSEO_VERSION' ) || defined( 'THE_SEO_FRAMEWORK_VERSION' ) || defined( 'SEOPRESS_VERSION' ) ) {
        return;
    }

    $description = '';

    if ( is_singular() ) {
        $post        = get_queried_object();
        $description = has_excerpt( $post ) ? $post->post_excerpt : wp_trim_words( excerpt_remove_blocks( strip_shortcodes( $post->post_content ) ), 35, '' );
    } elseif ( is_category() || is_tag() || is_tax() ) {
        $description = term_description();
    } elseif ( is_front_page() || is_home() ) {
        $description = get_bloginfo( 'description' );
    }

    $description = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( (string) $description ) ) );

    if ( '' !== $description ) {
        printf( "<meta name=\"description\" content=\"%s\" />\n", esc_attr( wp_html_excerpt( $description, 160, '' ) ) );
    }
}, 1 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Automatic Meta Description" 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/
