Override meta description written by Yoast SEO in PHP

Updated on October 7, 2025

Yoast SEO provides the wpseo_metadesc hook for modifying the meta description before it is written to the HTML <meta> tag. If you need to override the description in some way, you can write a function that receives the pending description and returns a different value.

The following example illustrates how to modify the description. This code adds a filter function to wpseo_metadesc. The filter function will be called just before Yoast writes the meta description.

add_filter('wpseo_metadesc', function($description) {

    // This example overrides Yoast for single post-type objects
    // such as a blog post, page, or custom post type.
    if (is_singular()) {
    
        // Get access to the current post being queried or displayed.
        global $post;
        
        // Implement your custom meta description here
        return your_code_or_function($post);
    }
    
    // Fallback to Yoast's description for other post-types
    return $description;
});

Reference

License

Licensed under CC BY 4.0

You are free to share and adapt this content for any purpose as long as you give appropriate credit in a reasonable manner.

No affiliate links

We do not participate in affiliate marketing, and we are not paid to mention products.

Leave a Reply

Your email address will not be published. Required fields are marked *