Generate meta description automatically in WordPress without plugin

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:

function add_custom_meta_des(){
if( is_home() || is_front_page() ){
$meta_des = "Your website description here";
echo '<meta name="description" content="' . $meta_des . '" />'; } }
add_action( 'wp_head', 'add_custom_meta_des', 4 );

Replace this “Your website description here” with your own website description.

To generate the meta description for single posts only, use this code in  functions.php in your child theme:

This part of the code for Single Posts has been updated on Appril 05 2019 due to an error identified in GitHub.

function create_meta_desc() {
 global $post;
if (!is_singular()) {return; }
elseif(!empty( $post->post_excerpt)) {
echo "<meta name='description' content='$post->post_excerpt' />";
} else{ $meta = apply_filters('the_content', $post->post_content);
	$meta = strip_tags($meta);
	$meta = strip_shortcodes($meta );
	$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
	$meta = substr($meta, 0, 200);
	echo "<meta name='description' content='$meta' />";
} } add_action('wp_head', 'create_meta_desc');

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:

function add_custom_meta_des(){
if( is_home() || is_front_page() ){
$meta_des = "Your website description here";
echo '<meta name="description" content="' . $meta_des . '" />'; } }
add_action( 'wp_head', 'add_custom_meta_des', 4 );

function create_meta_desc() {
 global $post;
if (!is_singular()) {return; }
elseif(!empty( $post->post_excerpt)) {
echo "<meta name='description' content='$post->post_excerpt' />";
} else{ $meta = apply_filters('the_content', $post->post_content);
	$meta = strip_tags($meta);
	$meta = strip_shortcodes($meta );
	$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
	$meta = substr($meta, 0, 200);
	echo "<meta name='description' content='$meta' />";
} } add_action('wp_head', 'create_meta_desc');

 

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.

Contact us: info@indexofapps.com