Here’s how to add meta description automatically in WordPress for homepage and single post. By adding a code in functions.php file, the meta description will be generated in the head section immediately when you publish your post.
Adding meta description automatically in WordPress through a plugin is easy. But using plugins to build the entire site structure is not recommended.
Below we will specify 3 php codes, one for the homepage, one for single post and another for both home page and single post, which is used as a single code for both cases.
To add the meta description for homepage only, use this code in functions.php in your child theme:
The code “$meta, 0, 200” defines the size of the meta description or where it starts (0) and where it ends (200). If you think it is better to decrease or enlarge, then you can change it.
I want to clarify something. I’m not even advanced in terms of codes, but I study things before I publish them. When I started writing this post I thought that merging codes for “Home” and “Single” would change something especially the last line, but I see it unnecessary.
So, just merge the two codes if you want the meta description to be generated for both, the homepage and the single post. However …
… the final code will look like this:
In this picture below we have explained how home and single post meta description is generated.
In the post you can see the first words, the bottom of the picture.
On the right side of the picture, see how the code is located in “functions.php”.
While on the left side of the photo, see how “meta description” appears in Google search results.
You can also use a SEO plugin such as Yoast SEO, Rank Math or any other plugin. This would make it much easier for you, not only for meta description but also for other SEO aspects. But before using a SEO plugin you need to plan the entire structure of your site.