How to Increase Auto Generated Excerpt for Longer Meta Descriptions for Yoast and WordPress

 In Yoast Tips

With the recent increase in Google using longer meta description, which are now showing upwards of 320 characters and more, SEO specialist and website developers that have relied on the auto generated %%excerpt%% in Yoast are faced with a real conundrum.  If you have the latest version of Yoast and you specify a meta description manually then you are in good shape and there is no need to continue reading.  If you are like us, and sometimes forget to specify a meta description, it is nice to rely on the auto generated option set up in Yoast.  But we do not want to be limited to the 55 word limit in WordPress

Do You Have Auto Excerpt even Set-up in Yoast?

Let’s set up the situation first.  If you have installed Yoast and have not specified the auto-excerpt in the meta description and you do not specify a description, your meta description most likely is missing all together.  Most developers think that Yoast will specify a description for you, which is false.  Go over to your Yoast Titles and Meta and check to see if a variable is missing in the meta description.

So let’s look at the variables that Yoast Gives us to work with.  Navigate the Need Help tab to expand the window and select the Template Explanation Tab.  Here you will see the variables you can use with Yoast.  If you look down some more you will see the advanced variables. If your Basic Variable will not work we will need to create a advanced variable.

To auto generate your meta description you would copy the %%excerpt%% variable and put it into your meta description box for your post typoes.  This will grab the first 55 words of the content and strip it of all html tags.

How Do we Change the Excerpt to be longer than 55 Words?

However, the auto-excerpt in WordPress only limits the text to 55 words. With Google allowing larger meta descriptions we would want to change that to stay in best practices.

For some users a simple function in the function.php file in a child theme would solve the problem.

Such as this function to extend the excerpt to 110 words in lieu of the standard 55:

function ltg_excerpt_length( $length ) {
return 110;
}
add_filter( 'excerpt_length', 'ltg_excerpt_length', 999 );

Depending on your Theme, WordPress version and Yoast version, this may not do the trick.

That Didn’t Work, Now What?

If that didn’t work for you then we would want to create a custom Variable in Yoast and use a function to take the post content, strip it of tags and limit it to 320 characters.  Then we will need to input the custom variable in the post types.

First things first.  Navigate over to your functions file in your child theme and add this code:

// Register Custom Variable in Yoast and increase Character limit
add_action('wp_head','add_custom_meta_description_box');

function custom_length_meta_desc ( $var1 ) {
global $post;
return substr(strip_tags($post->post_content), 0, 320); // change 320 to whatever character length you would like
}

function my_plugin_custom_variable () {
wpseo_register_var_replacement( ‘%%longer_excerpt%%’, ‘custom_length_meta_desc’, ‘advanced’, ‘Auto generate meta description from the content up to 320 characters’ );
}
add_action( ‘wpseo_register_extra_replacements’, ‘my_plugin_custom_variable’ );

Now let’s check to see if our Variable is in Yoast.

Check Yoast for Your Custom Variable

 

 

Now all we need to do is input our variable in the post fields to auto-generate a 320 character limit for your meta description.

 

 

Now let’s check Chrome Inspect to see if it specifies it for the posts that do not have a set meta description.

 

Start typing and press Enter to search