HOMEBlogTechnical SEODefault Meta Description: 3 Easy Steps to Set…

Default Meta Description: 3 Easy Steps to Set Fallbacks in Yoast SEO & Rank Math

Default Meta Description

Pages without an editor-written description can produce incomplete or unhelpful search snippets. Add a Default Meta Description safely by using an SEO plugin filter to generate a useful fallback only when its current description is empty; any description an editor has entered remains unchanged. This guide covers the Yoast SEO and Rank Math integrations independently, so you can use the filter that matches your site.

The fallback will use a post excerpt when one is available, or clean text from the post content when it is not. Before adding code, prepare a safe place to test it and confirm you can access the site files or a trusted PHP snippets manager. The implementation is scoped to individual posts and pages, not archives.

  • PHP 8.0 or later, or the PHP version required by your WordPress site and installed plugins.
  • A WordPress staging site or a recent backup that you can restore.
  • Yoast SEO or Rank Math installed and configured. Use only the integration for your active SEO plugin unless the site intentionally runs both.
  • Access to add PHP through a small site-specific plugin or a trusted snippets manager.
  • A published test post with an excerpt and a second published post without one.

Define the fallback behavior before coding

Set the precedence rule first: the fallback should run only when the SEO plugin provides an empty description. If an editor has entered a description in Yoast SEO or Rank Math, preserve it unchanged. This keeps editorial decisions authoritative and avoids replacing carefully written search copy with automatically generated text.

For this implementation, limit the behavior to individual posts and pages. Those singular views have a specific post object and content that can be used to produce a relevant fallback. Archives, search results, and other listing pages need separate, intentional description rules; do not apply a post-based generator to them by default.

Use a clear source order: take the post excerpt when it contains useful text, otherwise prepare a plain-text description from the post content. Remove markup, shortcodes, and excess whitespace before using that content. If neither source contains meaningful text, return an empty value rather than filling the description with a generic site tagline or unrelated copy.

A target around 155 to 160 characters is a practical editing guideline for the generated text, not a promise about what search engines will display. Search engines can shorten or replace descriptions depending on the query and display context.

Backup Required
Back up the site or test on staging before adding PHP that affects front-end output.

Add the snippet in a maintainable location

Choose where the helper and its SEO filter will live before adding code. For a production site, a small site-specific plugin is usually the most maintainable option: its behavior remains available if you change or update the active theme. Keep the code in one clearly named plugin file so you can review, disable, or roll it back without searching through unrelated theme features.

You can also use a trusted PHP snippets manager if it supports controlled activation and lets you recover safely from a syntax error. Create one snippet containing the shared helper and the filter for your SEO plugin. Enable only the Yoast SEO integration or the Rank Math integration that matches the plugin in use; do not register both simply because both examples are available.

A child theme functions.php file is another possible location, but it ties SEO behavior to that theme. If the child theme is replaced or becomes inactive, the fallback stops running, and the code can be harder to maintain alongside theme-specific functions. Prefer the site-specific plugin when the behavior should persist across theme changes.

The examples use the pnet_ prefix for every custom PHP function to reduce the risk of naming conflicts. Add the helper and its chosen filter together in the same location, and do not edit Yoast SEO or Rank Math plugin files: plugin updates can overwrite core-file changes. The next step defines the shared helper before showing each plugin connection separately.

Generate a Default Meta Description from post content

This shared helper prepares a plain-text fallback for an individual post or page. It uses the excerpt first, then cleaned post content if the excerpt is blank. It deliberately returns an empty string for archives, missing posts, and password-protected content, and does not substitute a generic site tagline when there is no useful text.

WordPress provides wp_strip_all_tags() to remove HTML and related markup. The helper also removes shortcodes, decodes entities, and normalizes whitespace before limiting the result to 158 characters. It uses multibyte string functions when available, with a Unicode-aware fallback.

PHP
function pnet_build_default_meta_description( $post_id ) {
    if ( ! is_singular( array( 'post', 'page' ) ) || ! $post_id ) {
        return '';
    }

    $post = get_post( $post_id );

    if ( ! ( $post instanceof WP_Post ) || '' !== $post->post_password ) {
        return '';
    }

    $source = trim( (string) $post->post_excerpt );

    if ( '' === $source ) {
        $source = (string) $post->post_content;
    }

    $source = strip_shortcodes( $source );
    $description = wp_strip_all_tags( $source, true );
    $charset = get_bloginfo( 'charset' );

    if ( '' === $charset ) {
        $charset = 'UTF-8';
    }

    $description = html_entity_decode( $description, ENT_QUOTES | ENT_HTML5, $charset );
    $description = wp_strip_all_tags( $description, true );
    $description = wp_check_invalid_utf8( $description, true );
    $description = preg_replace( '/\s+/u', ' ', $description );

    if ( ! is_string( $description ) ) {
        return '';
    }

    $description = trim( $description );

    if ( '' === $description ) {
        return '';
    }

    $limit = 158;

    if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_substr' ) ) {
        if ( mb_strlen( $description, 'UTF-8' ) > $limit ) {
            $description = mb_substr( $description, 0, $limit, 'UTF-8' );
        }
    } else {
        $characters = preg_split( '//u', $description, -1, PREG_SPLIT_NO_EMPTY );

        if ( is_array( $characters ) ) {
            if ( count( $characters ) > $limit ) {
                $description = implode( '', array_slice( $characters, 0, $limit ) );
            }
        } else {
            $description = substr( $description, 0, $limit );
        }
    }

    return trim( $description );
}

Add this helper to the same site-specific plugin or snippets entry as the SEO filter you choose. Because the helper checks the current request, call it from the plugin integration on a singular post or page; the next sections connect it to Yoast SEO and Rank Math separately.

Connect the helper to Yoast SEO

Yoast SEO provides the wpseo_metadesc filter for changing the description it outputs. Register a callback that checks the value Yoast passes in: if it contains non-whitespace text, return that value as-is. Only an empty value should trigger the shared helper, which uses the current post ID to build the fallback.

PHP
function pnet_yoast_default_meta_description( $description ) {
    if ( ! is_string( $description ) ) {
        return $description;
    }

    if ( '' !== trim( $description ) ) {
        return $description;
    }

    return pnet_build_default_meta_description( get_queried_object_id() );
}

add_filter( 'wpseo_metadesc', 'pnet_yoast_default_meta_description', 10, 1 );

Keep this filter beside the shared helper in the site-specific plugin or snippets entry you chose. The priority is 10, and the final argument, 1, tells WordPress to pass one value to the callback. Do not paste this into Yoast SEO files; plugin updates can replace those changes. If you use Rank Math instead, use its separate integration rather than registering this Yoast filter.

Default Meta Description - Yoast Fallback Preview
Yoast Fallback Preview

Connect the helper to Rank Math

Rank Math uses the rank_math/frontend/description filter to customize its front-end meta description. The callback below follows the same precedence rule as the Yoast integration: it returns any non-empty value Rank Math supplies exactly as received, and calls the shared helper only when that value is blank.

PHP
function pnet_rank_math_default_meta_description( $description ) {
    if ( ! is_string( $description ) ) {
        return $description;
    }

    if ( '' !== trim( $description ) ) {
        return $description;
    }

    return pnet_build_default_meta_description( get_queried_object_id() );
}

add_filter( 'rank_math/frontend/description', 'pnet_rank_math_default_meta_description', 10, 1 );

Place this callback and its filter registration alongside the shared pnet_build_default_meta_description() helper in your site-specific plugin or snippets entry. The registration uses priority 10 and accepts one argument. Keep the Yoast and Rank Math integrations separate; enable only the filter for the active SEO plugin unless both plugins are intentionally installed and configured.

Default Meta Description - Rank Math Fallback Preview
Rank Math Fallback Preview

Verify the generated description and editor override

Run these checks on staging before deploying the integration. Use published sample posts and inspect the rendered page source after each change. The goal is to confirm that a manually written SEO description remains authoritative and that the fallback appears only when the plugin has no description to output.

  1. Confirm the editor override. Enter a distinctive description in the active SEO plugin field, save the post, and view its page source. Find the <meta name="description"> element and confirm its content matches the value you entered. The filter must not replace it.
  2. Check the excerpt fallback. Clear the SEO description, add a written excerpt, and reload the page. The rendered description should contain the excerpt as cleaned plain text, limited by the helper.
  3. Check content fallback. Clear the excerpt and SEO field, leaving ordinary body content. Confirm the output uses cleaned content and contains no HTML markup, shortcode syntax, or repeated whitespace.
  4. Check cases with no suitable source. Try an empty-content post and a password-protected post; neither should receive an unsuitable generated value. Visit an archive as well: this helper intentionally does not create archive descriptions.
  5. Inspect the final output. View source rather than relying only on the browser inspector, and confirm there is one description meta element from the intended SEO plugin. A search engine may still rewrite or shorten the description shown in results, so its displayed snippet is not a reliable test of the page output.

Record the expected source for each test post—editor field, excerpt, content, or no fallback—so you can repeat the checks after plugin, theme, or PHP changes.

Pro Tip
Keep a small test matrix with the expected description source for each sample post so regressions are easy to spot after plugin or theme updates.

You might also like:

WordPress Last Updated Date: The Ultimate Guide to Display Post Modified Date

Want to improve SEO freshness? Learn how to show the WordPress last updated date instead of the published date. Read...

Read more →

Common Errors

When the output does not match expectations, first identify whether the issue is the incoming SEO description, the helper conditions, or another source adding a meta tag. Check the rendered page source after each change so you can distinguish plugin output from an editor preview or a search result.

A manually entered description still appears. That is the intended precedence rule. Check the active plugin’s description field and confirm it contains the value you expected before changing the filter. The fallback should not replace a non-empty editor-written description.

The generated value is blank. Confirm the request is a singular post or page and that the post is published and accessible. The helper also returns an empty value for password-protected posts. Add a usable excerpt or body text; an empty source should not produce invented copy.

The snippet causes a fatal error. Make sure the shared helper is declared only once, the PHP snippet is enabled, and the callback name in add_filter() exactly matches the declared function. If the site becomes unavailable, disable the snippet through your snippets manager or site-specific plugin before editing it again.

The description looks truncated. Search display lengths vary by language, device, and query, so a 158-character value is not guaranteed to display in full. Shorten the excerpt or adjust the helper target when the generated text is consistently cut at an awkward point.

More than one description tag appears. Confirm that only the intended SEO integration is active, then inspect the rendered source for output from a second SEO plugin or the theme. Remove the duplicate source rather than adding another filter.

Archives remain unchanged. This helper intentionally handles singular content only. If archive descriptions are needed, create a separate archive-specific strategy. Do not edit Yoast SEO or Rank Math core files: plugin updates can overwrite those changes.

You might also like:

Rank Math Custom Variables: 5 Easy Steps to Add Dynamic Meta Titles in WordPress

Rank Math Custom Variables missing from meta titles? Add them in WordPress with our step-by-step guide.

Read more →

Conclusion

Use one shared content-cleaning helper, then connect it to the filter for the SEO plugin installed on the site: Yoast SEO or Rank Math. The helper should prefer a useful excerpt, fall back to cleaned post content only when needed, and return no value when there is no suitable source. Generate a fallback only when the plugin passes an empty description. This keeps the output relevant without producing generic text for archives or protected content.

Preserving any non-empty description supplied by the plugin is essential: it lets editors retain control over intentional search copy. The code should support editorial work, not silently replace it. Keep the shared helper and the chosen integration together in a site-specific plugin or trusted snippets manager, and avoid editing SEO plugin files, where changes may be lost during updates.

Before deploying, test on staging with a manually written description, a post with an excerpt, one without an excerpt, and cases with no usable content. Confirm the editor value remains unchanged, the fallback contains clean text, and the rendered page source has one description tag from the intended plugin. Recheck after plugin or theme updates. Keep archive descriptions as a separate implementation if your site needs them.

This approach provides a maintainable Default Meta Description only where content and plugin output justify one, while leaving editorial choices and search-engine display decisions intact.

Abhik

🚀 Full Stack WP Dev | ☕ Coffee Enthusiast | 🏍️ Biker | 📈 Trader
Hi, I’m Abhik. I’ve been coding since 2007, a journey that began when I outgrew Blogger and migrated to a robust self-hosted stack. That transition introduced me to WordPress, and I’ve been building professional solutions ever since.

Leave a comment