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
- add_filter() – Function | Developer.WordPress.org – Developer documentation for the add_filter function.
- Yoast SEO Meta descriptions: API documentation | Yoast developer portal – Yoast SEO developer documentation.
Leave a Reply