Hello @yi100,
Thank you for contacting Rank Math support.
When you leave the meta description field blank, our plugin generates the description from the first paragraph. There is no way to change this behavior. You can use the following filter to create a custom variable that takes the first 160 words from the content:
add_action('rank_math/vars/register_extra_replacements', function () {
rank_math_register_var_replacement(
'custom_desc',
[
'name' => esc_html__('Custom Description', 'rank-math'),
'description' => esc_html__('Custom description generated from the content', 'rank-math'),
'variable' => 'custom_desc',
'example' => custom_desc_callback(),
],
'custom_desc_callback'
);
});
function custom_desc_callback()
{
$word_limit = 160;
$text = wp_strip_all_tags(get_the_content());
$words = explode(' ', $text);
if (count($words) > $word_limit) {
return implode(' ', array_slice($words, 0, $word_limit)) . '...';
}
return $text;
}
Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/
Once done, you can use the %custom_desc% variable in Single Post Description option of WP Dashboard > Rank Math SEO > Titles & Meta > Posts.
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.