I wanted to use the following function to override the Yoast meta description:

function custom_meta( $desc ){

    if (/* do your test here to check template or any other values*/) {
        $desc = "Change the description";

    return $desc;

However, Yoast SEO support recommends the Metadata API.

From this page, I understand I can use the wpseo_metadesc filter to adjust the Meta_Description_Presenter, but I am unsure how it is done; I am not a programmer.

I'd love some assistance to create some functions.php code that will grab the first 160 characters of the content if the meta description hasn't been set already.

Help appreciated. Steve

1 Answer 1


I think you're on the right track here. You can use the wpseo_metadesc filter to alter the meta description any way you want. Check out below code, maybe it'll help you with your function.

add_filter( 'wpseo_metadesc', 'my_custom_meta_description' );
function my_custom_meta_description($description) {
    if ( !$description || empty($description) ) {
        global $post;
        $content = get_the_content($post->ID);
        if ( $content && !empty($content) ) {
            $description = substr($content, 0, 160);
    return $description;

I haven't tested the function so you may need to customize it for your need.

  • The goal is to overwrite the description. So I mean you should remove if check for $description, because he will overwrite the field with the characters from the post content.
    – bueltge
    Mar 22 at 8:27
  • Thanks for a fantastic answer. I can award the bounty in 3 hours, I'm told.
    – Steve
    Mar 22 at 8:31
  • 1
    @bueltge, the goal was "grab the first 160 characters of the content if the meta description hasn't been set already". The IF checks if the description has already been set, otherwise it should be overwritten. So the IF is needed in this case if I'm correct.
    – DeltaG
    Mar 22 at 13:37
  • @Steve, You're very welcome! Have a nice day :)
    – DeltaG
    Mar 22 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.